How to add Line Numbers to Files in Linux/Unix

If your file is too lengthy, and it is a specific line you are interested  then you surely will like this commands to work with.

This commands will let you display line numbers in files.

  •  cat command

  •  nl command – Display each FILE to standard output (screen), with line numbers added.

  • Gnu less command – Less is a program similar to more , but which allows backward movement in the file as well as forward movement

  • awk command

Usage :

The cat command syntax is:

cat -n file 
cat -n /etc/hosts

The nl command syntax is:

nl file 
nl /etc/hosts 

The less command syntax is:

less -N file 
less -N /etc/hosts

The awk command syntax is:

awk '{ print FNR " " $0 }' file 
awk '{ print FNR " " $0 }' /etc/hosts 
awk '{ print FNR "t" $0 }' /etc/hosts
This entry was posted in linux. Bookmark the permalink.