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:

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: