1828228 Members
3014 Online
109975 Solutions
New Discussion

Replace string

 
ivychung2
Frequent Advisor

Replace string

If I want to replace a string in a file with other words,

eg .
the original file is as below ,

aaa bbb ccc ddd
111 222 333 444
### %%% &&& ***
aaa bbb ccc ddd
111 222 333 444
### %%% &&& ***


if I want to replace all string "aaa bbb ccc ddd" with "sss ttt uuu vvv" , the result become to below , can suggest how to do it ? thx

sss ttt uuu vvv
111 222 333 444
### %%% &&& ***
sss ttt uuu vvv
111 222 333 444
### %%% &&& ***
11 REPLIES 11
Rajeev  Shukla
Honored Contributor

Re: Replace string

try
sed 's/aaa bbb ccc ddd/sss ttt uuu vvv/' source_file > dest_file
ivychung2
Frequent Advisor

Re: Replace string

thx reply ,

but if I want to direct update the file , the above method seems generate a new file , how to do it ? thx.
Rajeev  Shukla
Honored Contributor

Re: Replace string

ok vi the file
and then press shift : so you are in command mode and then type
s/aaa bbb ccc ddd/sss ttt uuu vvv/g

Antonio Cardoso_1
Trusted Contributor

Re: Replace string

this will do it:
cp original_file /tmp/original_file.tmp
sed -e "s/aaa bbb ccc ddd/sss ttt uuu vvv/" /tmp/original_file.tmp > original_file
rm /tmp/original_file.tmp

see man sed for more.
Arturo Galbiati
Esteemed Contributor

Re: Replace string

Hi,
perl -pe "s/aaa bbb ccc ddd/sss ttt uuu vvv/g" -i your_file your_file

This will chnage teh string in Your_file without showing teh result on stdlist.

HTH,
Art
Nguyen Anh Tien
Honored Contributor

Re: Replace string

#vi filename

:1,$s/aaa bbb ccc ddd/sss ttt uuu vvv
that's all
HP is simple
Peter Nikitka
Honored Contributor

Re: Replace string

Hi,

use ex:

print 'g/aaa bbb ccc ddd/s//sss ttt uuu vvv/g\nw' | ex /tmp/file

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Hein van den Heuvel
Honored Contributor

Re: Replace string


>> but if I want to direct update the file , the above method seems generate a new file , how to do it ? thx.

Please note that all of the methods outlined really create a brand new file which happens to look much the same as the original.

If you truly want to update in place, then you need a tool like a C program or PERL script which opens the file in update mode: OPEN file, "+
Google for : +perl +"open for update"
But I don't think that's what you actually want.


Also... what is the real problem you are trying to solve?
your data pattern _suggests_ that this is not a random replace, but perhaps driven by information from an other file/source. If you share that with us, then maybe we can come up with a better solution/script for the larger problem.

Best regards,
Hein.



Peter Nikitka
Honored Contributor

Re: Replace string

Hi Hein,

I think my 'ex'-solution will - permissions granted - non-interactively produce a file with the same name as the original one but containing the requested changes (Art's solutions as well).

mfG Peter

The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Hein van den Heuvel
Honored Contributor

Re: Replace string


Well Peter, I know it is nitpicking, but I beg to differ
'ex' is just a nice userfriendlier jacket around the standard editor which takes care of the hidding the temp file. But I could be wrong

From the man page:
"Current file. The name of the file being edited by ex is called the current file. Text from the current file is read into a work area, and all editing changes are performed on this work area. Changes do not affect the original file until the work area is explicitly written back to the file. If the % character is used as a file name, it is replaced by the current file name."

So the whole buffer is wrtten into a new file, which happens to have the same name.

Ditto for perl. The -i argument tells Perl to make the change in-line using the file for both reading and writing. If a -i is followed by a file extensiona backup of the file is created before the program is run. But really that's taking place anyway.

Try it on a good sized (50+ MB?) file.
A true inplace replace would read the whole file and just write those blocks that have changed data. So for 50MB, then would be 6000 or so read IOs and say 10 write IOs. I don't think that's what 'ex' nor 'perl -i' will do.

A true in-place replace can also only be done if the data size does not change. (which would be the case in the example provided)

Again, I'm just nitpicking.
The topic author is 99.99% sure to be just looking for the 'ex' or 'perl -i' solution and is unlikely to mean a literal 'in place' where just the changed blocks get written to.

Cheers,
Hein.
Peter Nikitka
Honored Contributor

Re: Replace string

Hi Hein,

of course you are correct, and that's why I told '... produce a file with the same name as the original one ...'.
I should have added
... but the inode will be different.

I think there are others in this forum, who have written a 'change_string_in_file.c' to patch (binary) files and missed to check the lengths of the search and replace strings in their first attempt correctly...

BTW, ivychung2, did you proceed in your efforts in string replacement?

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"