Little Girl's Mostly Linux Blog

NumberLinesInTextFile

Number lines in a text file

This page was last updated on February 09, 2010.

Here are two methods to number the lines in a text file. One doesn’t count blank lines and the other does. This is the source file used in the examples:

Contents of sourcefile
apples

oranges

nl

  • This command does not count blank lines. It uses the letter n and the letter L rather the letter n and the number 1.
  • nl sourcefile > destinationfile
    • Example output:
    • Contents of destinationfile
      1 apples

      2 oranges

cat -n

  • This command counts all lines, including blank lines.
  • cat -n sourcefile > destinationfile
    • Example output:
    • Contents of destinationfile
      1 apples
      2
      3 oranges


Obligatory Happy Ending

And they all lived happily ever after. The end.

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Comment:

Create a free website or blog at WordPress.com.