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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sed to insert string after second comma 3

Status
Not open for further replies.

SP68

IS-IT--Management
May 22, 2005
18
0
0
US
Hi,

I have a text file that looks like this:

abcbs,adbsd,,,,,,
adsdfd,afdsf,,,,,,
adfsd13,1232dfsfe,,,,,,

And I want to insert a string after the second comma that looks like this:

abcbs,adbsd,string,,,,,
adsdfd,afdsf,string,,,,,
adfsd13,1232dfsfe,string,,,,,

Thanks,

SP
 

Why does it have to be SED? Is this a home work assignment? Tek-Tips frowns on asking for help on Home work Assignments.

Will there always be 2 strings followed by the 6 NULL fields.? a,b,,,,,, or could there be othe caase like....

a,,,d,,, or a,b,c,d,,,,

I mean could you simply assume you want String between the first 2 adjacent commas.

sed -e 's/,,/,string,/' Myfile > newfile

on the other hand if you don't know how many fields you will have populated

awk -F, '{printf("%s,%s,%s,%s,%s,%s,%s,...\n",$1,$2,string,$3,$4,$5,%6,...);}'

add more stuff in place of the ... Depending on how many fields you have. This assumes you have a fixed number of strings

Also I didn't know whether string is supposed to replace the 3rd value or slide it down the list.

I mean should

a,b,c,,,, become a,b,string,c,,,, or a,b,string,,,,



 
sed 's!,!,string!2' /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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top