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
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.
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.
Summary: Can someone help? I'm trying to pass an echo string over to sed in order for sed to insert it into a text file (test) as such: echo "echo "ps -ef | grep `pwd | awk -F/ '{print $5}'`/config | awk '{pr...
Summary: Hi, I am trying to do the following simple operation with sed, but it wouldnt work because sed doesn't allow me to use variables in the replace string. Anyway to do it ? testvar="TestFilename.#.txt" t...