They're both annoying. For use in pipelines that is, as they seem more geared towards typesetting. Which I've honestly never had a use for. Consider this input file:
$ cat > test.txt
one
two
three
five
Let's try to number every line:
$ nl test.txt
1 one
2 two
3 three
4 five
Ok, the leading spaces are annoying (why not use a single tab?), and are also produced with "cat -n". And why is not numbering blank lines the default?
How can you prefix every line, even non-blank ones, with the line number followed by a single tab? Turns out the answer is:
Why does cat need the ability to add line numbers anyway? It seems outside the scope of it's purpose. Next it'll need ability to translate Russian to English.
If I ever needed line numbering, I would output into a file and open it in a far more capable text editor/viewer that was designed for doing that well.
Indeed - `nl` should be preferable to `cat -n` for that reason even if for no others. People have been actually saying this since at least 1983 (http://harmful.cat-v.org/cat-v/).
I'm often surprised that cat is anywhere near as popular as it is. I think I've used it for actual file concatenation maybe two times in the past three years.
> That's because numbers look better when they are right justified.
They don't look better to a computer though. And that's what I don't get. Why are the defaults geared towards typesetting (human consumption) while cut/sort/join friendly output is so painful to produce?
This is a very common scenario when processing log files: line numbering, then sorting on another field followed by join/grep/head to grab a subset of interest, then recovering the original line numbers for the subset of interest.
Honestly half the time I end up doing this, just because it's the shortest invocation that comes to mind:
Yeah, I usually use awk or perl for this (depends on my mood). Just find it annoying that the unix program whose stated purpose is to number lines is basically dead to me.
Comments
cat -n
but nl is shorter :)
They're both annoying. For use in pipelines that is, as they seem more geared towards typesetting. Which I've honestly never had a use for. Consider this input file:
Let's try to number every line: Ok, the leading spaces are annoying (why not use a single tab?), and are also produced with "cat -n". And why is not numbering blank lines the default?How can you prefix every line, even non-blank ones, with the line number followed by a single tab? Turns out the answer is:
Horrible defaults. HORRIBLE.Why does cat need the ability to add line numbers anyway? It seems outside the scope of it's purpose. Next it'll need ability to translate Russian to English.
If I ever needed line numbering, I would output into a file and open it in a far more capable text editor/viewer that was designed for doing that well.
Indeed - `nl` should be preferable to `cat -n` for that reason even if for no others. People have been actually saying this since at least 1983 (http://harmful.cat-v.org/cat-v/).
I'm often surprised that cat is anywhere near as popular as it is. I think I've used it for actual file concatenation maybe two times in the past three years.
> Ok, the leading spaces are annoying (why not use a single tab?)
That's because numbers look better when they are right justified.
The blank line thing is stupid though.
> That's because numbers look better when they are right justified.
They don't look better to a computer though. And that's what I don't get. Why are the defaults geared towards typesetting (human consumption) while cut/sort/join friendly output is so painful to produce?
This is a very common scenario when processing log files: line numbering, then sorting on another field followed by join/grep/head to grab a subset of interest, then recovering the original line numbers for the subset of interest.
Honestly half the time I end up doing this, just because it's the shortest invocation that comes to mind:
I've never heard of your use case before--that is interesting.
Though I'm not sure I see why the "%5d" method is hard to parse...
seems to work ok. "cut -f 2" and "cut -c 8-" also seem to work well in this case.Yup, use awk for similar tasks:
Yeah, I usually use awk or perl for this (depends on my mood). Just find it annoying that the unix program whose stated purpose is to number lines is basically dead to me.
cat -n is discussed in Pike and Kernighan's paper on Unix style: http://harmful.cat-v.org/cat-v/