查找多文件内容替换

UPDATE: (05/03/2013)
当前使用:

#去掉所有的FactoryGirl.
find . -type f -name *_spec.rb -print | xargs  sed -ie 's/FactoryGirl\.//g'



//Seems like Linux is being far too generous.

-i 's/^[[:space:]]\{11\}//g' <-- makes it look like 
"s/^[[:space:]]\{11\}//g" is the extension given to "-i"

//baf's quotes serve to clarify to the interpreter (and to humans reading it) that
there is no extension. hayne's -e is also a means with which to disambiguate.

//Thus, these all work:
sed -i "" 's/^[[:space:]]\{11\}//g' menu1a.sh 
sed -i -e 's/^[[:space:]]\{11\}//g' menu1a.sh 
sed -ie 's/^[[:space:]]\{11\}//g' menu1a.sh 
sed -i "" -e 's/^[[:space:]]\{11\}//g' menu1a.sh

Makes sense.


find . -name "*.php" -print | xargs sed -i 's/foo/bar/g'

$ find /home/bruno/old-friends -type f -exec sed -i 's/ugly/beautiful/g' {} \;



引用


ere’s the post on my new blog – http://rushi.vishavadia.com/blog/find-replace-across-multiple-files-in-linux/
Below is an older version of the post:

I was trying to find a solution todo a find & replace across multiple files which was purely command line based. There are plenty of scripts out there which will accomplish this but I needed a single line command. After some google searches and some experimentation I came up with this snippet.

    find . -name "*.php" -print | xargs sed -i 's/foo/bar/g'

It looks a bit complicated but its quite simple. There are three components to the command:

    find . -name "*.php" -print – Find all files (recursively) which has “.php” in the file and print them out. This will give you output like this:

    ./file.php
    ./includes/test.php
    ./classes/class.php

    xargs- This command is used when you want to pass a lot of arguments to one command. xargs will combine the single line output of find and run commands with multiple
    arguments, multiple times if necessary to avoid the max chars per line limit. In this case we combine xargs with sed
    sed -i 's/foo/bar/g' – aka Stream Editor is a tool which should be in every sys admin’s toolkit.  In this case every occurence of “foor” is replaced by “bar” in all the files found using the “find” command. Sed simply parses input and applies certain text transformations to it. There’s a lot to say about sed, you can find more at this tutorial.

This pretty much covers the core of the find & replace command. You could also open up a particular folder in an IDE and use it’s find and replace feature. But find + sed is quite fast and powerful.

你可能感兴趣的:(sed)