Reputation: 11
I have a file and want to add numbers infront of it, below is an example:
I have a file with the following:
0.152
0.153
0.158
0.156
0.157
and I want to put increasing numbers infront of it with a space, like this:
1 0.152
2 0.153
3 0.158
4 0.156
5 0.157
ascendingnumber*space*numberinfile
I would be very greatful if anyone can help. I have a large amount of data so it would take me for ages to add the numbers in manually. Its Linux stuff.
Many Thanks A struggling student :)!
Upvotes: 0
Views: 100
Reputation: 40753
If your system has the nl command:
$ cat numbers.txt
0.152
0.153
0.158
0.156
0.157
$ nl -w 1 -s ' ' numbers.txt
1 0.152
2 0.153
3 0.158
4 0.156
5 0.157
The -w 1 flag specifies the column width of the ascending number. The -s ' ' flag tells nl to use one space to separate the numbers.
Upvotes: 1
Reputation: 25609
several ways
awk '{print NR,$0}' file
cat -n file
nl file
sed '=' file
ruby -ne 'print "#{$.} #{$_}"' file
Of course, just bash
c=1; while read -r line; do echo $((c++)) $line; done < file
Upvotes: 1