开发者

How do I do a recursive find & replace within an SVN checkout?

How do I find and replace every occurrence of:

foo

with

bar

in every text file under the /my/test/dir/ directo开发者_如何转开发ry tree (recursive find/replace).

BUT I want to be able to do it safely within an SVN checkout and not touch anything inside the .svn directories

Similar to this but now with the SVN restriction: Awk/Sed: How to do a recursive find/replace of a string?


There are several possiblities:

Using find:

Using find to create a list of all files, and then piping them to sed or the equivalent, as suggested in the answer you reference, is fairly straightforward, and only requires scanning through the files once.

You'd use one of the same answers as from the question you referenced, but adding -path '*/.svn' -prune -o after the find . in order to prune out the SVN directories. See this question for a discussion of using the prune option with find -- although note that they've got the pattern wrong. Thus, to print out all the files, you would use:

find . -path '*/.svn' -prune -o -type f -print

Then, you can pipe that into an xargs call or whatever to do the individual replacements, as suggested in the question you referenced. There is a lot of discussion there about different options, which I won't reproduce here, although I prefer the version from John Zwinck's answer:

find . -path '*/.svn' -prune -o -type f -exec sed -i 's/foo/bar/g' {} +

Using recursive grep:

If you have a system with GNU grep, you can use that to find the list of files as well. This is probably less efficient than find, but it does allow you to only call sed on the files that match, and I personally find the syntax a lot easier to remember (or figure out from manpages):

sed -i 's/foo/bar/g' `grep -l -R --exclude-dir='*/.svn' 'foo' .`

The -l option causes grep to only output the list of file names, rather than the matching lines.

Using a GUI editor:

Alternately, if you're using windows, do what I do -- get a copy of the NoteTab editor (available in a free version), and use its search-and-replace-on-disk command, which ignores hidden .svn directories automatically and just works.

Edit: Corrected find pattern to */.svn instead of .svn, added more details and some other possibilities. However, this depends on your platform and svn version: .svn without */ may be required in some cases, like on CentOS 7.


How about this?

grep -i "search_string" `find "*.some_extension"`

That is halfway solution to finding a search_string within files that have a specific extension....once you know the files that has the string, can be easily modified by piping it into sed....

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜