Unix Shell - Replace many occurrences of a string within a file.

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > September 2007 > Replace many occurrences of a string within a file.





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Replace many occurrences of a string within a file.
Kenneth Brun Nielsen

2007-09-14, 1:23 pm

I need to replace many occurrences of a string ("foo") with another
("bar") within the same file through UNIX command line.

I tried:
cat file.txt | sed 's/foo/bar/'

But this is not good, since only the first occurrence in a line is
replaced.

Do you have better (working) solutions?

/Kenneth

Spiros Bousbouras

2007-09-14, 1:23 pm

On 14 Sep, 17:24, Kenneth Brun Nielsen
<kenneth.brun.niel...@googlemail.com> wrote:
> I need to replace many occurrences of a string ("foo") with another
> ("bar") within the same file through UNIX command line.
>
> I tried:
> cat file.txt | sed 's/foo/bar/'
>
> But this is not good, since only the first occurrence in a line is
> replaced.
>
> Do you have better (working) solutions?


sed 's/foo/bar/g' < file.txt


Miles

2007-09-14, 1:23 pm

On Sep 14, 11:24 am, Kenneth Brun Nielsen
<kenneth.brun.niel...@googlemail.com> wrote:
> I need to replace many occurrences of a string ("foo") with another
> ("bar") within the same file through UNIX command line.
>
> I tried:
> cat file.txt | sed 's/foo/bar/'
>
> But this is not good, since only the first occurrence in a line is
> replaced.
>
> Do you have better (working) solutions?
>
> /Kenneth


try:

sed "s/foo/bar/g"


Kenneth Brun Nielsen

2007-09-14, 1:23 pm

> sed 's/foo/bar/g' < file.txt

So simple and so fast. The power of Usenet!

Thanks a lot, mate.

Cyrus Kriticos

2007-09-14, 1:23 pm

Spiros Bousbouras wrote:
> sed 's/foo/bar/g' < file.txt


sed 's/foo/bar/g' file.txt

--
Best regards | "The only way to really learn scripting is to write
Cyrus | scripts." -- Advanced Bash-Scripting Guide
Jim Jackson

2007-09-17, 1:29 pm

Kenneth Brun Nielsen <kenneth.brun.nielsen@googlemail.com> wrote:
[vbcol=seagreen]
> So simple and so fast. The power of Usenet!


even faster - man sed :-)

> Thanks a lot, mate.


Kenneth Brun Nielsen

2007-09-17, 1:29 pm

On Sep 17, 3:00 pm, j...@franjam.org.uk (Jim Jackson) wrote:
> Kenneth Brun Nielsen <kenneth.brun.niel...@googlemail.com> wrote:
>
>
> even faster - man sed :-)


Nahh. Not in this case - I'm pretty certain, that I couldn't do faster
than 2 minutes :-)

BTW, looking back, it seems more as a regexp problem than a sed
problem. But I simply forgot everything about the 'g'-option.

Tiago Peczenyj

2007-09-19, 7:19 pm

for large files use:

sed '/foo/s//bar/g' < file.txt

On Sep 17, 10:00 am, j...@franjam.org.uk (Jim Jackson) wrote:[vbcol=seagreen]
> Kenneth Brun Nielsen <kenneth.brun.niel...@googlemail.com> wrote:
>
>
> even faster - man sed :-)
>


Kevin

2007-09-21, 1:27 pm

On Sep 19, 12:31 pm, Tiago Peczenyj <tiago.pecze...@gmail.com> wrote:[vbcol=seagreen]
> for large files use:
>
> sed '/foo/s//bar/g' < file.txt
>
> On Sep 17, 10:00 am, j...@franjam.org.uk (Jim Jackson) wrote:
>
>
>
>

perl -p -i -e 's/foo/bar/g' file.txt
This actually writes the file with the replaced text.

Kevin

2007-09-21, 1:27 pm

On Sep 19, 12:31 pm, Tiago Peczenyj <tiago.pecze...@gmail.com> wrote:[vbcol=seagreen]
> for large files use:
>
> sed '/foo/s//bar/g' < file.txt
>
> On Sep 17, 10:00 am, j...@franjam.org.uk (Jim Jackson) wrote:
>
>
>
>

perl -p -i -e 's/foo/bar/g' file.txt
This actually writes the file with the replaced text.

fabdeb

2007-09-24, 1:26 pm

On Sep 21, 5:28 pm, Kevin <hodg...@gmail.com> wrote:
> On Sep 19, 12:31 pm, Tiago Peczenyj <tiago.pecze...@gmail.com> wrote:
>
>
>
>
>
>
>
>
> PERL -p -i -e 's/foo/bar/g' file.txt
> This actually writes the file with the replaced text.


hi,
you can also try sed -i 's/foo/bar/g' file.txt
It works fine for my debian.

Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com