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!

sed substitue question 1

Status
Not open for further replies.

kasparov

Programmer
Feb 13, 2002
203
GB
I have a file with records of 4 fields, each enclosed in double quotes & separated by commas. The first field may contain spaces, where it does I want the space to be converted to an underscore. Any spaces in other fields must remain.

I can figure out how to do this using awk (though my solution is very messy) but it must be possible to use sed to substitue only the spaces in the first field to underscores. Can anyone help? As an example here's what some lines may look like:

"1403078","00174013.tif","\\server name\directory name","Paybill162\134379"
"1403079","00174014.tif","\\server name\directory name","Paybill162\134379"
"1403080 1403086 1403087 1403088 1403089 1403090 1403091","00174015.tif","\\server name\directory name","Paybill162\134379"
"1403080 1403081 1403082 1403083 1403084 1403085 1403086 1403087 1403091","00174017.tif","\\server name\directory name","Paybill162\134379"

Thanks in advance
 
Try...

sed -e :a -e 's/\(^"[^"]*\) /\1_/;ta' file
 
Not sure how this would be done with straight sed, but here is my semi-creative solution for it:

for line in "`cat FILE`"
do
X=`echo ${line}|cut -d "," -f1`
Y=`echo ${X}|sed 's/ /_/g'`
echo "${line}"|sed "s/${X}/${Y}/" >> FILE2
done

Somewhat clean..., hope that helps.
 
A not too messy awk solution:

[tt]nawk 'BEGIN { FS=OFS="," } {gsub(" ","_",$1); print}' filename[/tt]


Annihilannic.
 
Thanks Annihilannic.

I'll go for your nawk solution!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top