Computing.Net > Forums > Unix > replacing fields in delimited file

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

replacing fields in delimited file

Reply to Message Icon

Name: u9717410
Date: September 3, 2004 at 07:09:35 Pacific
OS: hp ux 11
CPU/Ram: unknown
Comment:

I am trying to tag a number onto the front of a unique field in a datafile. At the moment i can do this to the second field in a file because it is always preceeded by a D and a pipe delimeter ie 'D|'. There fore i am using this where pIncrese is the number to be put on the front

sed "s/^D|\(.*\)|/D|${pIncrease}\1|/" \

I now want to be able to do the same thing but to the third field in another file, i tried this unsucessfully

sed "s/^D|\(.*\)|\(.*\)|/D|\(.*\)| \
${pIncrease}\1|/"

the third fourth fifth etc fields can only be identified by the number of '|' chracters preceeding them.

Can anyone help!




Sponsored Link
Ads by Google

Response Number 1
Name: Jim Boothe
Date: September 3, 2004 at 09:33:33 Pacific
Reply:

awk is easier for targeting selected fields.  I am assuming that you have fields delimited by pipe characters.

pIncrease=123

awk -F\| -v incr=$pIncrease '\
BEGIN {OFS="|"}
{$3=incr $3
 print}' filein


0
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: replacing fields in delimited file

replace data in a file www.computing.net/answers/unix/replace-data-in-a-file/6189.html

Awk to edit field in file www.computing.net/answers/unix/awk-to-edit-field-in-file/8482.html

Replacing values in a file www.computing.net/answers/unix/replacing-values-in-a-file/7683.html