Linux – automatic replace a string with a new one in multiple files

I did have to do a lots of modification in almost 600 files, and i did it using Linux SED:
grep -ilr ‘UA-1234567-1’ * | xargs -i@ sed -i ‘s/UA-1234567-1/UA-9876543-2/g’ @
what it really does is replace the string UA-1234567-1 with UA-9876543-2
🙂

2 comments

  1. Your use of xargs is dangerous. To see why read: http://en.wikipedia.org/wiki/Xargs#The_separator_problem
    $ echo UA-1234567-1 > “foo’bar'”
    $ grep -ilr ‘UA-1234567-1’ * | xargs -i@ sed -i ‘s/UA-1234567-1/UA-9876543-2/g’ @
    $ cat “foo’bar'”
    UA-1234567-1
    Consider using GNU Parallel http://www.gnu.org/software/parallel/ instead.
    $ grep -ilr ‘UA-1234567-1’ * | parallel sed -i ‘s/UA-1234567-1/UA-9876543-2/g’
    Watch the intro video to GNU Parallel at http://www.youtube.com/watch?v=OpaiGYxkSuQ

  2. Tnx for the info i did not know that for some Unix there are problms with xargs as long as the lines do not contain ‘, ” or space.

Leave a comment

Your email address will not be published.