linuxsed

Insert line number in a file


Would like to insert line number at specific location in file e.g.

apple 
ball

should be

(1) apple 
(2) ball

Using command sed '/./=' <FileName>| sed '/./N; s/\n/ /'

It generates

1 Apple
2 Ball

Solution

  • 1st solution: This should be an easy task for awk.

    awk '{print "("FNR") "$0}' Input_file
    


    2nd solution: With pure sed as per OP's attempt try:

    sed '=' Input_file | sed 'N; s/^/(/;s/\n/) /'