Operating System - HP-UX
1849835 Members
2722 Online
104044 Solutions
New Discussion

Help with sed!? (RTFF,RTFM I hear you cry.......)

 
SOLVED
Go to solution
Charles Harris
Super Advisor

Help with sed!? (RTFF,RTFM I hear you cry.......)

Dear all,

Quick question:-

Sed. I'm sure in my glory days, I used to use sed to do text replacement on files (nothing wrong so far)....but here's the clintcher.. without writing the stdout to another file or did I? - ie replacing something within a file without creating a new version via redirection of stout.

Any tips pointers warmly received as ever, even RTFM's although if I trawl another sed how-to I might explode.

Cheers,

-ChaZ-
6 REPLIES 6
Praveen Bezawada
Respected Contributor

Re: Help with sed!? (RTFF,RTFM I hear you cry.......)

Hi
with sed you cannot do text replacement inline :(. you have to use perl

...BPK...
Tom Geudens
Honored Contributor

Re: Help with sed!? (RTFF,RTFM I hear you cry.......)

Hi,
Without an intermediate file ... I don't think that's possible in a script. Closest I could get was
(sed 's/Hallo/hallo/' < file1 > /tmp/file1) && (cat /tmp/file1 > file1) && (rm -f /tmp/file1)

... which is not at all what you asked ...
Tom Geudens :-(
A life ? Cool ! Where can I download one of those from ?
harry d brown jr
Honored Contributor
Solution

Re: Help with sed!? (RTFF,RTFM I hear you cry.......)

Not really an issue:

for small files under 16k:

sed -e "s/oldstr/newstr/g" filename|tee >filename

for larger files, cheat:

dd if=filename bs=3000000|sed -e "s/oldstr/newstr/g" > filename

of course adjust the "bs" (block size accordingly to input file size)

live free or die
harry

Live Free or Die
Charles Harris
Super Advisor

Re: Help with sed!? (RTFF,RTFM I hear you cry.......)

Thanks for the reply's I cunningly dual posted this question with the wonder that is netscape and have received both your and their great reply's.


Thanks again!

-ChaZ-
Ps. I'll dish the points when netscape is back upto speed!
Marco Paganini
Respected Contributor

Re: Help with sed!? (RTFF,RTFM I hear you cry.......)

Hello Charles,

You cannot do it with sed. SED is the "Stream" ED. If you want to replace 'in place' you'll have to pipe commands to ed, like this:

echo "1,$ s/oldstring/newstring
w /tmp/testfile
q" | ed /tmp/testfile

This will change oldstring by newstring on /tmp/newfile

Regards,
Paga
Keeping alive, until I die.
Stefan Schulz
Honored Contributor

Re: Help with sed!? (RTFF,RTFM I hear you cry.......)

HI,

i don't think you can do it with sed. but i solved this problem with ex:

ex file_to_edit << EOF
1,\$s/oldstring/newstring/
w
q
EOF


Hope this helps

Regards Stefan
No Mouse found. System halted. Press Mousebutton to continue.