[prog] using sed on multiple files

Finne Boonen hennar at gmail.com
Sat Jul 9 05:24:18 EST 2005


---------- Forwarded message ----------
From: Finne Boonen <hennar at gmail.com>
Date: Jul 8, 2005 8:18 PM
Subject: Re: [prog] using sed on multiple files
To: Nicki Handy <nicki at virtualnicki.com>


the other suggestion is just a small script that you can run on the
command line, you just have to enter it exactly as it is written.

what it does is take all filenames in the current directory, (i in *),
then loop over those filenames (for ...; do)
and for each filename ($i, the $ means you're accessing a variable,
wich you have declared in the for line) do the replace operation with
sed (sed "s/exp1/exp2/ filename) and to write the output to a file,
the file will be called the same as the original file, but with -new
appended to it. in the next line (mv $i-new $i) you're moving the file
wich captured the output from sed ($i-new) to the original filename
($i).

If you have to do this recursively you can either do it manually over
several directories, or you can write a new loop (for ...; do .....
done;)

There's a usefull introduction link on my delicious bookmarks, you can
fnd it at: http://del.icio.us/hennar/bash

Finne

On 7/8/05, Nicki Handy <nicki at virtualnicki.com> wrote:
> Thanks, Finne
> I tried this but it is an illegal operation
>
> sed -i 's/TO 051201/TO 081201/g' filename
>
> maybe that command is not available anymore :(
>
> The other suggestion- is that perl? or shell? Do I need to write a
> script or would you just run that command line?
>
> Thanks a lot
> Nicki
>
> On Jul 8, 2005, at 12:38 PM, Finne Boonen wrote:
>
> > sed -i will change the file you're working on, without giving you
> > output, if that doesn't work:
> >
> > maybe:
> >
> > for i in *;
> >     do
> >     sed "s/exp1/exp2/" $i > $i-new;
> >     mv $i-new $i;
> > done;
> >
> > Finne
> >
> >
> > On  7/8/05, Nicki Handy <nicki at virtualnicki.com> wrote:
> >> Hi, I want to use the sed command on multiple files so that I can
> >> replace a piece of text in each of the files. The problem is the
> >> output- I can run the command fine on the whole directory but I don't
> >> want to have to specify the file to output, I just want it to change
> >> the file it's on.
> >>
> >> For example, this works.....
> >> machine# find ./ -type f -exec sed -e 's/051201/081201' {} \;
> >>
> >> But I need it to permanently make the change not just spit the output
> >> to STDOUT
> >> I can add a w but it needs to be dymanic
> >>
> >> # find ./ -type f -exec sed -e 's/051201/081201/w' {} \;
> >>
> >> Anyone know how to accomplish this?
> >>
> >> Thanks,
> >> Nicki

-- 
"Maybe you knew early on that your track went from point A to B, but
unlike you I wasn't given a map at birth!" Alyssa, "Chasing Amy"
http://hekla.rave.org/cookbook.html - my crossplatform dieet/recipe app


More information about the Programming mailing list