but nl is shorter :)
$ 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:
$ nl -w1 -n ln -ba -s"`printf '\t'`" test.txt
Horrible defaults. HORRIBLE.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.
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.
The blank line thing is stupid though.
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:
perl -lape '$_="$.\t$_"'Though I'm not sure I see why the "%5d" method is hard to parse...
nl /usr/share/dict/words | head -20 | sort -n
seems to work ok. "cut -f 2" and "cut -c 8-" also seem to work well in this case. awk '{ print FNR "\t" $0 }' test.txt
1 one
2 two
3 three
4
5 five