Re: sed question

From: rakesh sharma (sharma__r_at_hotmail.com)
Date: 02/19/04


Date: 19 Feb 2004 09:37:13 -0800

nasirmahmood@sat.net.pk (Nasir) wrote in message

> I have question on sed
> I have mutiple file containing IP address "66.132.176.100" and now I
> want to change all those matching files to replace this IP with
> another of let's say 66.132.176.99
> What I did was
>
> for i in `find -type f -exec 'grep' '-H' '66.132.176.100' '{}' \;`
> do
> sed 's/66\.132\.176\.100/66\.132\.176\.99/' < $i > $i
> done
>

You can accomplish all of the above within the 'find' command itself:

/bin/find . -type f -exec sh -c '
   grep -l "66\\.132\\.176\\.100" "$1" > /dev/null 2>&1 && \
   sed -e "s:\\(\\<66\\.132\\.176\\.\\)100\\>:\\199:" "$1" > "/tmp/$1" && \
   mv -f "/tmp/$1" .
' {} {} \;



Relevant Pages

  • Re: sed question
    ... > want to change all those matching files to replace this IP with ... You need a temporary file: ... The && operator will execute the next command only if the ...
    (comp.unix.shell)
  • Re: Need efficient search and replace for huge repository.....
    ... > or smiliar ought to do it. ... if there are a lot of matching files then errors may start ... using the find command won't have that trouble - either use the 'exec' ... the find command output to xargs and execute a command for each matching ...
    (comp.os.linux.misc)
  • Re: using find with grep
    ... >>But I would ALSO like it to display the directory ... >>for each find occurence. ... >>The command below works, but won't give me ... will only report the names of the matching files. ...
    (Debian-User)
  • Re: find and cat help needed
    ... > I am using this command to search through an old queue and reinject ... > matching files into my new queue using the appropriate qmail command: ...
    (comp.unix.shell)
  • Re: find and cat help needed
    ... >> I am using this command to search through an old queue and reinject ... Instead matching files should be individually be cat'ed ... >> into the qmail command. ...
    (comp.unix.shell)