Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Append a sequence at the begnining of a line 1

Status
Not open for further replies.

sachrath

Programmer
May 21, 2004
17
CH
Hi ,

I have a file with ',' as delmiter and structure as

x,345,rty,456,yyy
x,345,rty,456,yyy
x,345,rty,456,yyy
x,345,rty,456,yyy
x,345,rty,456,yyy
x,345,rty,456,yyy

I need to append a sequence number as the first character of the line by line number , so that the output should look like ,

1,x,345,rty,456,yyy
2,x,345,rty,456,yyy
3,x,345,rty,456,yyy
4,x,345,rty,456,yyy
5,x,345,rty,456,yyy


Any ideas, Is it possible through sed command ?

Regds
Sachin Rath
 
If you can live without sed, here a way with awk:
awk '{print NR","$0}' /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Another way:
pr -n,1 -t /path/to/input > output

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Hi

Is an ugly solution, must be two steps. But you asked for [tt]sed[/tt]...
Code:
sed '=' /path/to/input | sed 'N;s/\n/,/' > output
Note, that I use GNU [tt]sed[/tt]. Some other implementations seems to not have the [tt]N[/tt] command.

Feherke.
 
Hi:

Since we're posting ugly solutions:

Code:
sed 's/^/,/' filename|cat -n|sed 's/^[ \t]*//' |tr -d '\011'
 
Hi

Trojan said:
No ruby solutions then anyone?
Why not ?
Code:
ruby -pe 'print $.,","' /path/to/input > output
[gray]# or[/gray]
ruby -pe '$_.gsub!("^",$..to_s+",")' /path/to/input > output
[gray]# or[/gray]
ruby -pe '$_=$..to_s+","+$_' /path/to/input > output
But let have Perl too.
Code:
perl -pe 'print "$.,"' /path/to/input > output
[gray]# or[/gray]
perl -pe 's/^/$.,/' /path/to/input > output
[gray]# or[/gray]
perl -pe '$_="$.,$_"' /path/to/input > output
Boring sunday, isn't it ?

Feherke.
 
Hi,

Just use nl ( number lines ) with -s switch to define separator

Code:
nl -s, infile > outfile

 
Thanks aau - there's one I didn't know about.

Columb Healy
 
The correct use of nl to meet the OP request:
nl -s, -w1 infile > outfile

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top