Ryan Dinsmore
Ryan Dinsmore

Reputation: 11

Adding increasing numbers infront of a set of results

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

Answers (4)

Hai Vu
Hai Vu

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

kurumi
kurumi

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

James.Xu
James.Xu

Reputation: 8295

use awk

awk '{print NR " " $0}' input.txt > output.txt

Upvotes: 1

Šimon T&#243;th
Šimon T&#243;th

Reputation: 36441

cat file | awk '{ print NR " " $1 }'

Upvotes: 1

Related Questions