很多时候,我们需要进行多个文件的查找并替换,虽然IDE有这样的可视化功能,但是偏爱终端的人还是想要尝试用脚本实现一把。如下是一个简单的脚本来实现多文件的查找替换处理。
1 2 3 4 5 6 7 8 | #!/bin/sh # $1 search_keyword # $2 replace_original # $3 replace_destination # $4 search file type find ./ -type f -name "*.$4" -exec grep -l "$1" {} \; | xargs sed -i "" -e "s/$2/$3/g" |
---|
我们将上述脚本保存为replaceText.sh
。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | ➜ octopress git:(master) replaceText.sh "FD" "FD" "文件描述符" "markdown" ➜ octopress git:(master) ✗ git status On branch master Your branch is up to date with 'origin/master'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: source/_posts/2014-02-16-fix-no-sound-issue-on-mac.markdown modified: source/_posts/2014-07-06-my-plan-for-the-left-half-of-2014.markdown modified: source/_posts/2019-06-02-file-descriptor-explained.markdown modified: source/_posts/2019-06-09-will-unclosed-stream-objects-cause-memory-leaks.markdown modified: source/buy/index.markdown modified: source/fuli/index.markdown |
---|
这样一个很简单快速的功能就实现了。
注:该脚本未在Linux发行版验证,可能有涉及到sed的简单修改。
以上。