Skip to content

Comment on When to use "cat -n" instead of "wc -l"

Comments

I have always used 'nl' for numbering lines. That's how I learnt it from a Unix book around 2000 and the habit has stuck with me. And it's specified in POSIX: https://pubs.opengroup.org/onlinepubs/9799919799/utilities/n...

In fact, I wasn't aware of the 'cat -n' option until now. It appears to be supported by both BSD cat and GNU cat, although not specified in POSIX: https://pubs.opengroup.org/onlinepubs/9799919799/utilities/c...

Both examples in the post produce identical output when run through 'nl'. Here are a few commands to confirm this:

  diff <(seq 3 14 | cat -n) <(seq 3 14 | nl)
  diff <(look . | grep '^.b..t$' | cat -n) <(look . | grep '^.b..t$' | nl)
Aside: Since I'm talking about POSIX here, it's worth mentioning that process substitution using '<(commands)' is not specified in POSIX, but it's supported in bash, zsh, ksh93, etc.
AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.