经常会碰到这样的情况:查找某个目录下所有包含某个字符串的所有文件,并将这些文件中的这个字符串用另外的字符串替换进行替换。这种情况下,网网要检查的文件比较多,逐一进行检查替换太麻烦,这个时候,我们就应该找一个能够一条命令解决问题的方法。
1、grep命令
grep pattern file.txt命令默认的行为是将file.txt文件中,匹配pattern的行输出到标准输出。这个功能能帮助我们在文件中查找一个字符串出现的上下文,但是并不能够帮助我们实现下一步复杂的操作,所以有必要稍微了解下grep的一些选项。
现在测试文件的内容如下:
$ cat target.txt
<a class="navbar-brand" href="http://yyy.xxx.edu.cn/">Panda Search</a>
text-decoration: none;" href="http://www.asdf.net/scholar/xindong_wu.html">Xindong
" href="http://asdf.net/scholar/Federi
<a class="navbar-brand" href="http://yyy.xxx.edu.cn/">Panda Search</a>
href="http://www.asdf.net/scholar/Gong-Qing_Wu.html">
href="http://asdf.net/scholar/Federico_Bocardi.html">Federico occardi</a></li><span class="Apple-converted-space"> </span><li style="display: inline-block; padding-right: 5px; padding-left: 5px;"><a style="color: rgb(66, 139
为了测试我们将这个文件,另外拷贝出两份,重命名然后放在如下的目录结构中:
$ tree .
.
├── a
│ └── target1.txt
├── target2.txt
└── target.txt
执行grep命令:
$ grep -rn "http://yyy.xxx.edu.cn" *
a/target1.txt:1:<a class="navbar-brand" href="http://yyy.xxx.edu.cn/">Panda Search</a>
a/target1.txt:5: <a class="navbar-brand" href="http://yyy.xxx.edu.cn/">Panda Search</a>
target2.txt:1:<a class="navbar-brand" href="http://yyy.xxx.edu.cn/">Panda Search</a>
target2.txt:5: <a class="navbar-brand" href="http://yyy.xxx.edu.cn/">Panda Search</a>
target.txt:1:<a class="navbar-brand" href="http://yyy.xxx.edu.cn/">Panda Search</a>
target.txt:5: <a class="navbar-brand" href="http://yyy.xxx.edu.cn/">Panda Search</a>
grep提供下面的选项:*就是一般的bash通配符,表示当前目录所有文件,当然,你也可以写某个文件名
-r 是递归查找,意思就是也查找当前目录的子目录中的文件
-n 是显示行号
-i 忽略大小写
-l 列出匹配的文件名
-L 列出不匹配的文件名
-w 只匹配整个单词,而不是字符串的一部分(如只匹配‘man’,包括man两边有符号的如‘.man.’,或者是‘=man=’而不包括‘ woman ’或者是‘ manly ’)
$ echo `ls`
a target2.txt target.txt t.tt
$ echo $(ls)
a target2.txt target.txt t.tt
2.2 文件替换sedsed -i "s/old/new/g" file1.txt file2.txt 可以将file*.txt中的所有old换成new。如果new什么都没有,就是表示删除old的意思。3、将上面的技能串起来
$ sed -i "s/http:\/\/yyy.xxx.edu.cn//g" $(grep -lr "http://yyy.xxx.edu.cn" *)
$ cat target.txt
<a class="navbar-brand" href="/">Panda Search</a>
text-decoration: none;" href="http://www.asdf.net/scholar/xindong_wu.html">Xindong
" href="http://asdf.net/scholar/Federi
<a class="navbar-brand" href="/">Panda Search</a>
href="http://www.asdf.net/scholar/Gong-Qing_Wu.html">
href="http://asdf.net/scholar/Federico_Bocardi.html">Federico occardi</a></li><span class="Apple-converted-space"> </span><li style="display: inline-block; padding-right: 5px; padding-left: 5px;"><a style="color: rgb(66, 139
$ sed -i "s/http:\/\/www.asdf.net//g" $(grep -lr "http://www.asdf.net" *)
$ sed -i "s/http:\/\/asdf.net//g" $(grep -lr "http://asdf.net" *)
$ cat target.txt
<a class="navbar-brand" href="/">Panda Search</a>
text-decoration: none;" href="/scholar/xindong_wu.html">Xindong
" href="/scholar/Federi
<a class="navbar-brand" href="/">Panda Search</a>
href="/scholar/Gong-Qing_Wu.html">
href="/scholar/Federico_Bocardi.html">Federico occardi</a></li><span class="Apple-converted-space"> </span><li style="display: inline-block; padding-right: 5px; padding-left: 5px;"><a style="color: rgb(66, 139
$