Replace many occurrences of a string within a file.
Replace many occurrences of a string within a file.
am 14.09.2007 18:24:52 von Kenneth Brun Nielsen
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
Re: Replace many occurrences of a string within a file.
am 14.09.2007 18:26:43 von Spiros Bousbouras
On 14 Sep, 17:24, Kenneth Brun Nielsen
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
Re: Replace many occurrences of a string within a file.
am 14.09.2007 18:27:18 von Miles
On Sep 14, 11:24 am, Kenneth Brun Nielsen
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"
Re: Replace many occurrences of a string within a file.
am 14.09.2007 18:29:37 von Kenneth Brun Nielsen
> sed 's/foo/bar/g' < file.txt
So simple and so fast. The power of Usenet!
Thanks a lot, mate.
Re: Replace many occurrences of a string within a file.
am 14.09.2007 19:45:31 von Cyrus Kriticos
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
Re: Replace many occurrences of a string within a file.
am 17.09.2007 15:00:56 von jj
Kenneth Brun Nielsen wrote:
> > sed 's/foo/bar/g' < file.txt
> So simple and so fast. The power of Usenet!
even faster - man sed :-)
> Thanks a lot, mate.
Re: Replace many occurrences of a string within a file.
am 17.09.2007 17:39:43 von Kenneth Brun Nielsen
On Sep 17, 3:00 pm, j...@franjam.org.uk (Jim Jackson) wrote:
> Kenneth Brun Nielsen wrote:
>
> > > sed 's/foo/bar/g' < file.txt
> > So simple and so fast. The power of Usenet!
>
> 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.
Re: Replace many occurrences of a string within a file.
am 19.09.2007 21:31:39 von Tiago Peczenyj
for large files use:
sed '/foo/s//bar/g' < file.txt
On Sep 17, 10:00 am, j...@franjam.org.uk (Jim Jackson) wrote:
> Kenneth Brun Nielsen wrote:
>
> > > sed 's/foo/bar/g' < file.txt
> > So simple and so fast. The power of Usenet!
>
> even faster - man sed :-)
>
> > Thanks a lot, mate.
Re: Replace many occurrences of a string within a file.
am 21.09.2007 17:28:05 von Kevin
On Sep 19, 12:31 pm, Tiago Peczenyj wrote:
> for large files use:
>
> sed '/foo/s//bar/g' < file.txt
>
> On Sep 17, 10:00 am, j...@franjam.org.uk (Jim Jackson) wrote:
>
> > Kenneth Brun Nielsen wrote:
>
> > > > sed 's/foo/bar/g' < file.txt
> > > So simple and so fast. The power of Usenet!
>
> > even faster - man sed :-)
>
> > > Thanks a lot, mate.
perl -p -i -e 's/foo/bar/g' file.txt
This actually writes the file with the replaced text.
Re: Replace many occurrences of a string within a file.
am 21.09.2007 17:28:27 von Kevin
On Sep 19, 12:31 pm, Tiago Peczenyj wrote:
> for large files use:
>
> sed '/foo/s//bar/g' < file.txt
>
> On Sep 17, 10:00 am, j...@franjam.org.uk (Jim Jackson) wrote:
>
> > Kenneth Brun Nielsen wrote:
>
> > > > sed 's/foo/bar/g' < file.txt
> > > So simple and so fast. The power of Usenet!
>
> > even faster - man sed :-)
>
> > > Thanks a lot, mate.
perl -p -i -e 's/foo/bar/g' file.txt
This actually writes the file with the replaced text.
Re: Replace many occurrences of a string within a file.
am 24.09.2007 16:45:26 von fabdeb
On Sep 21, 5:28 pm, Kevin wrote:
> On Sep 19, 12:31 pm, Tiago Peczenyj wrote:
>
> > for large files use:
>
> > sed '/foo/s//bar/g' < file.txt
>
> > On Sep 17, 10:00 am, j...@franjam.org.uk (Jim Jackson) wrote:
>
> > > Kenneth Brun Nielsen wrote:
>
> > > > > sed 's/foo/bar/g' < file.txt
> > > > So simple and so fast. The power of Usenet!
>
> > > even faster - man sed :-)
>
> > > > Thanks a lot, mate.
>
> 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.