DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Find And Replace Text From The Shell

10.12.2007
| 6083 views |
  • submit to reddit
        Snagged from http://snippets.dzone.com/posts/show/116

find . -name '*.txt' -print0 |xargs -0 perl -pi -e 's/find/replace/g'
    

Comments

Snippets Manager replied on Fri, 2008/06/20 - 6:48pm

hello all I'm having this issue... when I execute this: # perl -p -w -e 's/(?:\n)(.*)//g' test1.txt I'm getting this error: Bareword found where operator expected at -e line 1, near "s/(?:\n)(.*)(?:\n)(.*)

Snippets Manager replied on Thu, 2007/11/15 - 3:16pm

Why even bother with xargs? Recursive solution perl -p -w -e "s/FIND/REPLACE/g;" -i $(find BASE_DIRECTORY -type f)

Snippets Manager replied on Wed, 2006/05/17 - 8:01pm

I like to do things like this with replace: find . -name '*.txt' | xargs replace 'find' 'replace' --

Snippets Manager replied on Mon, 2007/10/08 - 4:59pm

Why not use sed? find . -name '*.txt' -exec sed -i -e 's/find/replace/g' {} \; You can use sed for multiple replacements, too find . -name '*.txt' -exec sed -i -e 's/find1/replace1/g' -e 's/find2/replace2/g' -e 's/find3/replace3/g' ... {} \; I wonder which version is faster sed or perl??