Computing.Net > Forums > Unix > String Replacement w/ Sed

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.

String Replacement w/ Sed

Reply to Message Icon

Name: kenp
Date: August 6, 2003 at 17:34:06 Pacific
OS: Solaris
CPU/Ram: 512
Comment:

I'm typing to use Sed to search a file for a certain pattern, once it has found a row containing that pattern I want to move over a certain number of fields (delimited by commas) and then I want to replace whatever is in that field with something else. For example I want to find "computer" and then change the third field from 300 to 400:

1234,computer,300 --to-> 1234, computer, 400

Should I even be trying this with Sed? Thanks for any help.

-Ken



Sponsored Link
Ads by Google

Response Number 1
Name: nails
Date: August 6, 2003 at 21:43:05 Pacific
Reply:

Ken:

I'd use a pattern matching language like perl or awk:

# use nawk on Solaris
nawk ' { /computer/
$3=400
print $0
} ' file

Regards,

Nails


0

Response Number 2
Name: kenp
Date: August 7, 2003 at 10:13:55 Pacific
Reply:

I guess I forgot to mention this but how do I make this change permanent? How do I write this new line back to the same file that I opened and searched through, in other words, how do I overwrite the old line?

-Ken


0

Response Number 3
Name: nails
Date: August 7, 2003 at 12:27:40 Pacific
Reply:

Ken:

a way:

# use nawk on Solaris
nawk ' { /computer/
$3=400
print $0
} ' file > tmp.file
mv tmp.file file


0

Response Number 4
Name: Frank
Date: August 11, 2003 at 23:52:12 Pacific
Reply:

or use the ed,
this will avoid the handling of seperate temp file.


/bin/ed - 1>/dev/null
s//
w
q
EOF


No RISK no fun
Frank


0

Sponsored Link
Ads by Google
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: String Replacement w/ Sed

passing echo string over to sed www.computing.net/answers/unix/passing-echo-string-over-to-sed/6190.html

Shell script to replace a string www.computing.net/answers/unix/shell-script-to-replace-a-string/4987.html

pattern matching/replacing with sed www.computing.net/answers/unix/pattern-matchingreplacing-with-sed/6671.html