Advertisement

sed?

Started by May 26, 2004 06:39 AM
2 comments, last by Sir Valeq 20 years, 5 months ago
Hi! I''ve got a file with a structure like this: 1:Eeee Rrr:Ttt Yyyy: 2:Rrrr Ffff:Ggggg hhhh jjj: etc. What should I do in sed to: - find the line with a specific number in the beginning - write a string at the end of this line (after the last ":")? I beg for help.... -------------- "We cannot all be masters." - William Shakespeare
--------------"We cannot all be masters." - William Shakespeare
Not sed but works anyway:

to find line '5' and write 'foo' at the end (and save the original unmodified file as .bak):


perl -pi.bak -e 's/^5(.*):$/5$1:foo/' file.txt

HTH

[edited by - grazer on May 26, 2004 1:41:39 PM]
Advertisement
Here's some sed that works on Windows XP.
First, if you want just the one line with the number that you specify (not the rest of the file) try this:
sed -n "s/^2\(:.*:\)$/2\1sed is fun/p" "in.txt"  

Prints
2:Rrrr Ffff:Ggggg hhhh jjj:sed is fun  


If you want the whole thing but with the one line modified, try
sed "s/^2\(:.*:\)$/2\1sed is fun/" "in.txt"  


In the first example, the -n suppresses printing, and the p after the last / prints lines with a match. I use the GNU sed manual (at http://www.gnu.org/software/sed/manual/html_mono/sed.html) for a reference.

Enjoy.

[edited by - Metaphorically on May 26, 2004 7:25:17 PM]
Thanks, Metaphorically.
However I''ve already solved my problem, though a bit way around. I simplified it, but maintaining its job.
The funny thing is, that in the system (a UNIX) I''m doing it on there''s an undocumented parameter for sed: -i. This parameter has sed modify a file, without printing to the screen. I don''t have to use any ''w'' option with it. Strange, but actually nice.
Still, thanks for your post, since I always love to see some regular expressions working. I have a test (bash & awk) next week and I take any chance of learning anything.

--------------
"We cannot all be masters." - William Shakespeare
--------------"We cannot all be masters." - William Shakespeare

This topic is closed to new replies.

Advertisement