Linux在所有文件中查找和替换

经常会碰到这样的情况:查找某个目录下所有包含某个字符串的所有文件,并将这些文件中的这个字符串用另外的字符串替换进行替换。这种情况下,网网要检查的文件比较多,逐一进行检查替换太麻烦,这个时候,我们就应该找一个能够一条命令解决问题的方法。

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 ’)

2、Linux其他预备技能
2.1 将命令输出作为参数
方式有好多种,这里列出两种:``和$(),下面是一个例子。
$ echo `ls`
a target2.txt target.txt t.tt
$ echo $(ls)
a target2.txt target.txt t.tt
2.2 文件替换sed
sed命令的使用参见前面的文章,这里只说用到的地方:
sed -i "s/old/new/g" file1.txt file2.txt 可以将file*.txt中的所有old换成new。如果new什么都没有,就是表示删除old的意思。
3、将上面的技能串起来
这样,只需几行,就能够完成所有文件中,指定几种字符串的替换了。当然,也要注意下,sed命令的使用中,当有‘/‘字符的时候要注意转义,具体如下:
$ 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
$
看到这里,或许你也明白我要做什么了。其实,是这样的,一位童鞋往我们线上的系统中加了几个静态网页,网页中用到的超链全部是直接从浏览器复制过来的,包括站内的跳转。这样明显不科学,尽管当时是没毛病,但是现在我们的域名要换了,问题就来了,这些老域名的跳转链接都不能访问了。为了根治这个问题,我决定将这些站内跳转的域名部分全部删掉,就能够实现正常的跳转了。由于文件太多,只好用了上面的方法。

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。