7

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

Ramaprakasha
  • 231
  • 3
  • 10

2 Answers2

10

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
Lekensteyn
  • 174,277
4

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

glenn jackman
  • 17,900