find a string pattern in files and replace it - Unix

This is a discussion on find a string pattern in files and replace it - Unix ; Hi, I searched a file system and found a string pattern in several files. I want to change that string to other string. Can anyone shed some light on how to do it. Thanks in advance. Mike...

+ Reply to Thread
Results 1 to 5 of 5

Thread: find a string pattern in files and replace it

  1. find a string pattern in files and replace it

    Hi,
    I searched a file system and found a string pattern in several files. I
    want to change that string to other string. Can anyone shed some light
    on how to do it.
    Thanks in advance.
    Mike


  2. Re: find a string pattern in files and replace it

    Mike wrote:
    > Hi,
    > I searched a file system and found a string pattern in several files. I
    > want to change that string to other string. Can anyone shed some light
    > on how to do it.
    > Thanks in advance.
    > Mike
    >


    #!/bin/sh
    pattern="foo"
    replacement="bar"
    find . -exec grep -l "$pattern" {} \; |
    while read file
    do
    echo "replacing $pattern by $replacement in file $file"
    cp -p "$file" "$file.orig"
    sed "s/$pattern/$replacement/g" < "$file.orig" > "$file"
    # only if you are sure # rm "$file.orig"
    done

    --
    Michael Tosch @ hp : com

  3. Re: find a string pattern in files and replace it


    Mike wrote:
    > Hi,
    > I searched a file system and found a string pattern in several files. I
    > want to change that string to other string. Can anyone shed some light
    > on how to do it.
    > Thanks in advance.
    > Mike


    You can probably do this with bash and awk. Use a small for loop that
    iterates through the files that you need and then call an awk command
    to replace the string you need on each of the files as it goes through.

    This is a decent explanation of awk and replacing stuff.
    http://www.student.northpark.edu/pem...k/gawk_str.htm


  4. Re: find a string pattern in files and replace it

    Michael Tosch wrote:
    > Mike wrote:
    > > I searched a file system and found a string pattern in several files. I
    > > want to change that string to other string. Can anyone shed some light
    > > on how to do it.

    >
    > #!/bin/sh
    > pattern="foo"
    > replacement="bar"
    > find . -exec grep -l "$pattern" {} \; |
    > while read file
    > do
    > echo "replacing $pattern by $replacement in file $file"
    > cp -p "$file" "$file.orig"
    > sed "s/$pattern/$replacement/g" < "$file.orig" > "$file"
    > # only if you are sure # rm "$file.orig"
    > done


    Ah, and then the "excitement" when someone does something like:
    $ mkdir 'bar
    > ' 'bar
    > /etc'

    $ echo foo >'bar
    > /etc/passwd'


    http://www.rawbw.com/~mp/unix/sh/#Go...ming_Practices


  5. Re: find a string pattern in files and replace it

    On 19 Sep 2006 11:07:03 -0700, Mike wrote:
    > Hi,
    > I searched a file system and found a string pattern in several files. I
    > want to change that string to other string. Can anyone shed some light
    > on how to do it.

    [...]

    I generally do (using GNU grep, GNU xargs and perl):

    grep -rlZ pattern /some/dir | xargs -r0 perl -pi -e '
    s/pattern/replacement/g'

    --
    Stephane

+ Reply to Thread