Linux在所有文件中查找和替换
来源:互联网 发布:java企业员工管理系统 编辑:程序博客网 时间:2024/06/05 20:37
经常会碰到这样的情况:查找某个目录下所有包含某个字符串的所有文件,并将这些文件中的这个字符串用另外的字符串替换进行替换。这种情况下,网网要检查的文件比较多,逐一进行检查替换太麻烦,这个时候,我们就应该找一个能够一条命令解决问题的方法。
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.tt2.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$
看到这里,或许你也明白我要做什么了。其实,是这样的,一位童鞋往我们线上的系统中加了几个静态网页,网页中用到的超链全部是直接从浏览器复制过来的,包括站内的跳转。这样明显不科学,尽管当时是没毛病,但是现在我们的域名要换了,问题就来了,这些老域名的跳转链接都不能访问了。为了根治这个问题,我决定将这些站内跳转的域名部分全部删掉,就能够实现正常的跳转了。由于文件太多,只好用了上面的方法。
0 0
- Linux在所有文件中查找和替换
- Linux查找和替换目录下所有文件中字符串
- 查找和替换目录中所有文件中的某个字符串
- 查找和替换目录中所有文件中的某个字符串
- Ubuntu中的在文件中查找和替换命令
- Ubuntu中的在文件中查找和替换命令
- 在LoadRunner中查找和替换字符串
- 在LoadRunner中查找和替换字符串
- linux 查找、替换文件
- Linux vim中查找和替换指令
- linux中在多个文件中查找替换文本的shell脚本
- linux查找所有文件中某个字符串
- linux在多文件中查找关键词的位置以及替换关键词的方法
- linux在文件夹中查找文件包含的字符串,并进行相应的替换
- linux在文件夹中查找文件包含的字符串,并进行相应的替换
- linux查找和替换
- Linux中如何在所有文件中查找特定的字符串
- 替换字符串在一个文件夹中所有的文件
- UEditor 1.2.5 for jsp的使用
- 九度题目1393:合并两个排序序列
- Android WebView Memory Leak WebView内存泄漏
- gem5简介
- poj 1915 Knight moves
- Linux在所有文件中查找和替换
- 星球上最详细的AWR解析报告
- Android手机拨打电话的开发实例
- Linux LB 集群知识、如何用 LVS 方式实现 LB 集群?
- mysql数据的备份还原三种方法
- Eclipse 安装插件的三种方法
- Web服务器压力测试工具http_load、webbench、ab、Siege使用教程
- Linux _Namespaces的制度、管理、破译!
- 确认桌面钮与360智能点手,渺茫的智能键能够带给我们什么惊喜呢?