Append line numbers at end of each line in the file

In a peculiar case I needed some of my text files to append with line number at the end. How to achieve this?


The awk program stores the current line in variable $0 and the record number ("line number") in NR. This command will print the lines with a line number appended:

awk '{ print $0, NR }' yourfile.txt

For prepending line numbers, there also exist the nl command ("number lines of files"):

nl yourfile.txt

There will be a million ways to do this. Here's another one:

sed -n 'p;=' file | paste -d":" - -

paste's default delimiter is tab, if you omit the -d option