News for hackers. Stuff that matters.
Re:Cool, but obscure Unix tools by chanux on Friday May 20, 2011 @01:26PM (#2568604)
cat -n

but nl is shorter :)

Re:Cool, but obscure Unix tools by sigil on Saturday May 21, 2011 @03:45AM (#2570450)
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:

  $ nl -w1 -n ln -ba -s"`printf '\t'`" test.txt
Horrible defaults. HORRIBLE.
Re:Cool, but obscure Unix tools by jshb on Saturday May 21, 2011 @04:43AM (#2570504)
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.

Re:Cool, but obscure Unix tools by AlecSchueler on Saturday May 21, 2011 @08:36PM (#2572250)
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.

Re:Cool, but obscure Unix tools by __david__ on Saturday May 21, 2011 @04:44AM (#2570506)
> 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.

Re:Cool, but obscure Unix tools by sigil on Saturday May 21, 2011 @05:28AM (#2570561)
> 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$_"'
Re:Cool, but obscure Unix tools by __david__ on Saturday May 21, 2011 @07:50AM (#2570713)
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.
Re:Cool, but obscure Unix tools by nuxi on Saturday May 21, 2011 @11:32AM (#2571005)
Yup, use awk for similar tasks:

  awk '{ print FNR "\t" $0 }' test.txt
  1	one
  2	two
  3	three
  4	
  5	five
Re:Cool, but obscure Unix tools by sigil on Saturday May 21, 2011 @05:18PM (#2571769)
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.
Re:Cool, but obscure Unix tools by uriel on Friday May 20, 2011 @01:50PM (#2568741)
cat -n is discussed in Pike and Kernighan's paper on Unix style: http://harmful.cat-v.org/cat-v/