Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2004
    Posts
    143

    Unanswered: find and replace in unix

    Hi,
    Does anyone have a unix script that will find a string and replace it with an other string in all the files right under the directory from where this is run...and also all the files in the subdirectories. For example, I would like to replace the word mydb1 to mydb2 in all .sh files and subdirectory files, how do i get this done? Any suggestions on this would be greatly appreciated. Thanks

  2. #2
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    Try this :

    Code:
    find . -name '*.sh' | xargs perl -p -i -e 's/mydb1/mydb2/g'
    If you want to keep a save (*.sh_save) of your files :

    Code:
    find . -name '*.sh' | xargs perl -p -i'_save' -e 's/mydb1/mydb2/g'
    Jean-Pierre.

  3. #3
    Join Date
    Feb 2004
    Posts
    143
    Thank you Aigles. Worked perfect. If you can help me tweak this a little further, that would be wonderful.

    Keeping the same thing in place ,
    ( find . -name '*.sh' | xargs perl -p -i'_save' -e 's/mydb1/mydb2/g' )


    suppose, in place of mydb1, i want to include a string with slashes -
    something like /usr/limo/bin and replace it with /usr/limo9i/bin,
    how do i do this ? The reason I am doing this is because there are directory paths included within scripts and I would prefer to provide the whole path so that I don't replace anything that is unintended. Thanks a lot

  4. #4
    Join Date
    Jan 2004
    Location
    Bordeaux, France
    Posts
    320
    Code:
    find . -name '*.sh' | xargs perl -p -i'_save' -e 's+/usr/limo/bin+/usr/limo9i/bin+g'
    You can use any character in place of / (+ in this example) like you can do with sed.
    Jean-Pierre.

  5. #5
    Join Date
    Feb 2004
    Posts
    143
    Thanks a bunch

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •