How to add line numbers in every line using shell command?
The right tool for this job is nl
:
nl -w2 -s'> ' file
You may want to tune w
idth option according to the total number of lines in the file (if you want numbers to be aligned nicely).
Output:
1> PSS-A (Primary A)
2> PSS-B (Primary B)
3> PSS-C (Primary C)
4> PSS-D (Primary D)
5> PSS-E (Primary E)
6> PSS-F (Primary F)
7> PSS-G (Primary G)
8> PSS-H (Primary H)
9> PSS-I (Primary I)
10> SPARE (SPARE)
If you want the same format that you have specified
awk '{print NR "> " $s}' inputfile > outputfile
otherwise, though not standard, most implementations of the cat
command can print line numbers for you (numbers padded to width 6 and followed by TAB in at least the GNU, busybox, Solaris and FreeBSD implementations).
cat -n inputfile > outputfile
Or you can use grep -n
(numbers followed by :
) with a regexp like ^
that matches any line:
grep -n '^' inputfile > outputfile