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

using sed to alter the third field only 2

Status
Not open for further replies.

mfearnuk

Technical User
Apr 10, 2001
10
GB
Hi,

How can you subsitute only one field with sed - I don't want to replace all occurrences of a string?

Regards

Martin
 
Hello, Martin!

Sed doesn't recognize fields inside input line, I think. But awk does.

For example, if you want to replace the contens of the third field in input line with its logarithm, you can use this awk command:

awk '{ $3 = log($3); print }' inputfile > outputfile

I hope this helps.

Bye!

KP.
 
Thanks,

However I have lost my field seperator in the outputfile

awk -FS\| awk '{ $3 = "00000000"); print }' inputfile > outputfile


Inputfile
00001007|00719CC0|00000008|00000000|00000000|03036732

outputfile
00001007 00719CC0 00000000 00000000 00000000 03036732

:)

Martin
 
Martin ... this should do the trick. You need to set the Output Field Separator (OFS) as well.

awk 'BEGIN {FS="|";OFS="|"} {$3="00000000";print}' inputfile > outputfile

Greg.
 
thanks, Greg! Examples worked great. One thing, if you're doing numeric manipulations on a string that's not a number, awk tends to add extra fields. One way I got around this is specifying the input FS in the command line,

awk -F '|' {OFS="|"}{etc...}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top