Shell Script To Number Lines Of Files

Q. How do I number each line of a text file? How do I write a shell script to display text file with line numbers added?

A. You don’t have to write a shell script to add numbers to text file or code sample. You can use nl command which write each FILE to screen with line numbers added.


nl command example

Type the command as follows:
$ nl /etc/passwd
Sample output:

     1  root:x:0:0:root:/root:/bin/bash
     2  daemon:x:1:1:daemon:/usr/sbin:/bin/sh
     3  bin:x:2:2:bin:/bin:/bin/sh
     4  sys:x:3:3:sys:/dev:/bin/sh
     5  sync:x:4:65534:sync:/bin:/bin/sync
     6  games:x:5:60:games:/usr/games:/bin/sh
     7  man:x:6:12:man:/var/cache/man:/bin/sh
     8  lp:x:7:7:lp:/var/spool/lpd:/bin/sh
     9  mail:x:8:8:mail:/var/mail:/bin/sh
    10  news:x:9:9:news:/var/spool/news:/bin/sh
    40  quagga:x:119:126:Quagga routing suite,,,:/var/run/quagga/:/bin/false
    41  oracle:x:1004:1004::/usr/lib/oracle/xe:/bin/bash
    42  telnetd:x:112:120::/nonexistent:/bin/false

nl command has few more options:
$ nl --help

Usage: nl [OPTION]... [FILE]...
Write each FILE to standard output, with line numbers added.
With no FILE, or when FILE is -, read standard input.

Mandatory arguments to long options are mandatory for short options too.
  -b, --body-numbering=STYLE      use STYLE for numbering body lines
  -d, --section-delimiter=CC      use CC for separating logical pages
  -f, --footer-numbering=STYLE    use STYLE for numbering footer lines
  -h, --header-numbering=STYLE    use STYLE for numbering header lines
  -i, --page-increment=NUMBER     line number increment at each line
  -l, --join-blank-lines=NUMBER   group of NUMBER empty lines counted as one
  -n, --number-format=FORMAT      insert line numbers according to FORMAT
  -p, --no-renumber               do not reset line numbers at logical pages
  -s, --number-separator=STRING   add STRING after (possible) line number
  -v, --first-page=NUMBER         first line number on each logical page
  -w, --number-width=NUMBER       use NUMBER columns for line numbers
      --help     display this help and exit
      --version  output version information and exit

By default, selects -v1 -i1 -l1 -sTAB -w6 -nrn -hn -bt -fn.  CC are
two delimiter characters for separating logical pages, a missing
second character implies :.  Type \\ for \.  STYLE is one of:

  a         number all lines
  t         number only nonempty lines
  n         number no lines
  pBRE      number only lines that contain a match for the basic regular
              expression, BRE

FORMAT is one of:

  ln   left justified, no leading zeros
  rn   right justified, no leading zeros
  rz   right justified, leading zeros
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

12 comments… add one
  • Halttula Apr 18, 2008 @ 11:06

    You can do that with cat also.
    cat -n

  • Tobi Apr 18, 2008 @ 12:30

    I used cat -n till now. Not so much features but enough for using it in some scripts.

  • 🐧 nixCraft Apr 18, 2008 @ 13:01

    Tobi / Halttula: thanks for pointing out cat -n switch.

  • Vitaly May 1, 2008 @ 7:22

    “pr” can do it too

  • budacsik Sep 20, 2008 @ 14:48

    grep -n
    but this not so good

  • seaman77 Feb 22, 2009 @ 9:23

    Hello, I’m new in linux, please help me to write this shell script and explain them
    1- Write a shell script ( that :
    – Take as command-line options any number of text files
    -Read in each of these files, converts all the letters to uppercase, and then stores the results in a file of the same name but with a .caps extension

    2- Write a script ( that will take a single filename as an argument and adds execute permission to the file for the user, but only if the file is a regular file.
    Your script must check to see that there is exactly one argument. If there are no arguments or more than one arguments, your script must produce a “usage” message that tells how to use the script.

  • Tarun Jul 16, 2009 @ 21:47

    How would you connect from one UNIX terminal to another using SSH can any one explain with example.

  • 🐧 nixCraft Jul 17, 2009 @ 6:05

    use ssh:

  • Tarun Jul 23, 2009 @ 19:30

    How would you make your umask setting permanent?

  • sunardo Feb 24, 2010 @ 3:05

    how to remove number each line of a text file?

  • Abdullah Nawash Jun 21, 2011 @ 13:57

    You can do that with cat also.
    cat -b

  • kiers Mar 21, 2013 @ 9:52

    How do I REMOVE line numbers from a text file which has line numbers starting on every line?

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.