Hacker News new | comments | ask | show | jobs | submit login

I recently discovered nl and felt sad about the amount of time I had wasted figuring out how to add line numbers to files.

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:

  $ cat > test.txt

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.

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:

  perl -lape '$_="$.\t$_"'

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...

  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.

Yup, use awk for similar tasks:

  awk '{ print FNR "\t" $0 }' test.txt
  1	one
  2	two
  3	three
  5	five

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/

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact