+2 votes
in Programming Languages by (7k points)

I want to add the record number to each of the records in my file. 

E.g. if these are the first two records in my file

jan 300

feb 250

They should become

1 jan 300

2 feb 250

1 Answer

0 votes
by (13.5k points)

You can try the following AWK command:

awk '{print NR,$0}' input_file >output_file

For example:

Here is my input file 'testfile.txt':

$ cat testfile.txt
jan 300
feb 250
mar 100
apr 400

I ran the following awk command:

$ awk '{print NR,$0}' testfile.txt >testfile1.txt

My output file (testfile1.txt) has the following records:

$ cat testfile1.txt
1 jan 300
2 feb 250
3 mar 100
4 apr 400

...