How to add line numbers in every line using shell command?
Solution 1
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
Solution 2
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)
Solution 3
In Linux/Unix, there are almost always multiple ways to do common tasks. Just for completeness, here are some other ways you can do it besides the obvious:
pr -t -n [file]
From an old command to format text to send to a line printer. The '-t' will omit header and footer information that are not relevant to a terminal.
Here's a cute sed method that prints the line number on every other line. We use 'paste' to fold them into a single line:
sed = /etc/passwd | paste - -
Or, we can use the one true editor, ed:
echo '1,$n' | ed -s [file]
Or, ex, vi's non-cursor-addressing predecessor:
printf 'set number\ng/^/p\n' | ex /etc/passwd
And one final complicated answer, requiring ksh93 or bash (and the seq command. Using the .. range and an eval statement is left as an exercise):
paste <(seq $(wc -l < [file])) [file]
Tested on Debian Linux, FreeBSD, and Solaris 10 (the last fails there because of no 'seq').
Related videos on Youtube
Nainita
Updated on September 18, 2022Comments
-
Nainita over 1 year
My file,
PSS-A (Primary A) PSS-B (Primary B) PSS-C (Primary C) PSS-D (Primary D) PSS-E (Primary E) PSS-F (Primary F) PSS-G (Primary G) PSS-H (Primary H) PSS-I (Primary I) SPARE (SPARE)
Output file,
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)
-
Nainita over 8 yearsYes...both the commands are working.... but in
cat
command its printing the line numbers... but not exactly what I wanted.... butawk '{print NR "> " $s}' inputfile > outputfile
gives me the desired output.... :-) @amit kumar -
vinc17 over 5 years
-
myrdd over 5 years@StéphaneChazelas indeed, thanks a lot! Note that
$'...'
syntax is bash-specific. -
Sridhar Sarnobat over 5 yearsMy heart sank when I saw that
seq
didn't do it. Thank god fornl
-
Stéphane Chazelas about 5 years@myrdd,
$'...'
comes from ksh93 and is also supported byzsh
,mksh
, busybox sh, FreeBSD sh and bash at least. It's not standard yet, but is planned for inclusion in the next major POSIX version. -
myrdd about 5 years@StéphaneChazelas thanks. for reference, there's a question on
$'...'
(ANSI-C Quoting) portability: unix.stackexchange.com/questions/371827/… -
Onnonymous almost 5 yearsI like how
nl
can set a starting number with-v
. -
xeruf almost 4 yearsplease add an explanation, I'd love to do this with sed, but I don't understand the command :)