From: John W. Krahn on
Rakesh Sharma wrote:
> On Nov 22, 12:13 pm, miloody <milo...(a)gmail.com> wrote:
>> Dear all:
>> suppose i want to replace cat with dog in all the files within a
>> folder, how could I reach that with grep and sed?
>> i try to use "grep -lr 'cat' * |sed -ei 's/cat/deg/' ", but sed seems
>> cannot eat the file names piped by grep.
>> How could I pass the file names to sed?
>> appreciate your help,
>> miloody
>
>
> You could do this many ways:
>
> find . \
> \! -name . -prune
> -type f \
> -exec \
> grep -li 'cat' {} \;
> -exec \
> sed -ie 's/cat/dog/g' {} \;
>
> for _f in ./* ./.[!.]* ./..?*; do
> if [ -f "$_f" ] && [ ! -L "$_f" ]; then
> if grep -li 'cat' < $_f > /dev/null; then
> sed -ie 's/cat/dog/g' "$_f"
> fi
> fi
> done
>
> perl -e '
> for (<./* ./.[!.]* ./..?*>) {
> system("perl -i.BAK -0777e '/cat/ && s//dog/g' \"$_\"") if ! -l

You don't need both a match operator and a substitution operator.

> && -f _;
> }
> '

And you don't have to start a separate process to run the substitution:

perl -e'
$^I = ".BAK";
local $/;
for ( grep !-l && -f _, <./* ./.[!.]* ./..?*> ) {
@ARGV = $_;
s/cat/dog/g while <>;
}
'

Or also as:

perl -i.BAK -0777pe's/cat/dog/g' ./* ./.[!.]* ./..?*



John
--
The programmer is fighting against the two most
destructive forces in the universe: entropy and
human stupidity. -- Damian Conway
From: Rakesh Sharma on
On Dec 2, 3:40 pm, "John W. Krahn" <some...(a)example.com> wrote:
> Rakesh Sharma wrote:
> > On Nov 22, 12:13 pm, miloody <milo...(a)gmail.com> wrote:
> >> Dear all:
> >> suppose i want to replace cat with dog in all the files within a
> >> folder, how could I reach that with grep and sed?
> >> i try to use "grep -lr 'cat' * |sed -ei 's/cat/deg/' ", but sed seems
> >> cannot eat the file names piped by grep.
> >> How could I pass the file names to sed?
> >> appreciate your help,
> >> miloody
>
> > You could do this many ways:
>
> > find . \
> >    \! -name . -prune
> >    -type f  \
> >    -exec \
> >       grep -li 'cat' {} \;
> >    -exec \
> >       sed -ie 's/cat/dog/g' {} \;
>
> > for _f in ./* ./.[!.]* ./..?*; do
> >   if [ -f "$_f" ] && [ ! -L "$_f" ]; then
> >     if grep -li 'cat' < $_f > /dev/null; then
> >        sed -ie 's/cat/dog/g' "$_f"
> >     fi
> >   fi
> > done
>
> > perl -e '
> >   for (<./* ./.[!.]* ./..?*>) {
> >      system("perl -i.BAK -0777e '/cat/ && s//dog/g' \"$_\"") if ! -l
>
> You don't need both a match operator and a substitution operator.
>
> > && -f _;
> >   }
> > '
>
> And you don't have to start a separate process to run the substitution:
>
> perl -e'
>    $^I = ".BAK";
>    local $/;
>    for ( grep !-l && -f _, <./* ./.[!.]* ./..?*> ) {
>       @ARGV = $_;
>       s/cat/dog/g while <>;
>    }
> '
>
> Or also as:
>
> perl -i.BAK -0777pe's/cat/dog/g' ./* ./.[!.]* ./..?*
>
> John
> --
> The programmer is fighting against the two most
> destructive forces in the universe: entropy and
> human stupidity.               -- Damian Conway- Hide quoted text -
>
> - Show quoted text -

Thanks John for your masterly explanations.!

Has it ever crossed your mind (or O'Reilly's) to gather your perl-
one-
liner solutions from all over comp.unix.shell, comp.perl, etc.
& put them in a book form?

--Rakesh