Linux在所有文件中查找和替换
系统大全为您提供
经常会碰到这样的情况:查找某个目录下所有包含某个字符串的所有文件,并将这些文件中的这个字符串用另外的字符串替换进行替换。这种情况下,网网要检查的文件比较多,逐一进行检查替换太麻烦,这个时候,我们就应该找一个能够一条命令解决问题的方法。
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><><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 "stp://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><><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 "stp://www.asdf.net//g" $(grep -lr "http://www.asdf.net" *)
$ sed -i "stp://asdf.net//g" $(grep -lr "http://asdf.net" *)
$ cat target.txt
<a class="navbar-brand" href="/">Panda Search</a>
text-decoration: none;" href="/scholardong_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><><span class="Apple-converted-space"> </span><li style="display: inline-block; padding-right: 5px; padding-left: 5px;"><a style="color: rgb(66, 139
$
看到这里,或许你也明白我要做什么了。其实,是这样的,一位童鞋往我们线上的系统中加了几个静态网页,网页中用到的超链全部是直接从浏览器复制过来的,包括站内的跳转。这样明显不科学,尽管当时是没毛病,但是现在我们的域名要换了,问题就来了,这些老域名的跳转链接都不能访问了。为了根治这个问题,我决定将这些站内跳转的域名部分全部
以上就是系统大全给大家介绍的如何使的方法都有一定的了解了吧,好了,如果大家还想了解更多的资讯,那就赶紧点击系统大全官网吧。
本文来自系统大全http://www.win7cn.com/如需转载请注明!推荐:win7纯净版
系统下载排行榜71011xp
番茄花园 Win7 64位 快速稳定版
2【纯净之家】Windows7 64位 最新纯净版
3【老机专用】Windows7 32位 精简旗舰版
4【纯净之家】Windows7 SP1 64位 全补丁旗舰版
5JUJUMAO Win7 64位 最新纯净版
6【纯净之家】Windows7 32位 最新纯净版
7【纯净之家】Windows7 SP1 64位 办公旗舰版
8【雨林木风】Windows7 64位 装机旗舰版
9【电脑公司】Windows7 64位 免费旗舰版
10【国庆献礼】JUJUMAO Win7 SP1 64位旗舰克隆纯净版
【纯净之家】Windows10 22H2 64位 专业工作站版
2【纯净之家】Windows10 22H2 64位 游戏优化版
3【纯净之家】Windows10 企业版 LTSC 2021
4【纯净之家】Windows10企业版LTSC2021 纯净版
5【深度技术】Windows10 64位 专业精简版
6联想Lenovo Windows10 22H2 64位专业版
7JUJUMAO Win8.1 Up3 X64 极速精简版v2019.11
8【纯净之家】Windows10 22H2 64位 专业精简版
9【雨林木风】Windows10 64位 专业精简版
10【纯净之家】Windows10 22H2 64位 专业办公版
【纯净之家】Windows11 23H2 64位 游戏优化版
2【纯净之家】Windows11 23H2 64位 专业工作站版
3【纯净之家】Windows11 23H2 64位 纯净专业版
4风林火山Windows11下载中文版
5【纯净之家】Windows11 23H2 64位 最新企业版
6【纯净之家】Windows11 23H2 64位 中文家庭版
7JUJUMAO Win11 24H2 64位 专业版镜像
8深度精简 Win11 ESD 全新优化纯净版V2024
9【纯净之家】Windows11 23H2 64位专业精简版
10【纯净之家】Windows11 23H2 64位 纯净家庭版
深度技术 GHOST XP SP3 电脑专用版 V2017.03
2深度技术 GGHOST XP SP3 电脑专用版 V2017.02
3Win7系统下载 PCOS技术Ghost WinXP SP3 2017 夏季装机版
4萝卜家园 GHOST XP SP3 万能装机版 V2017.03
5番茄花园 GHOST XP SP3 极速体验版 V2017.03
6雨林木风 GHOST XP SP3 官方旗舰版 V2017.03
7电脑公司 GHOST XP SP3 经典旗舰版 V2017.03
8雨林木风GHOST XP SP3完美纯净版【V201710】已激活
9萝卜家园 GHOST XP SP3 完美装机版 V2016.10
10雨林木风 GHOST XP SP3 精英装机版 V2017.04
热门教程
装机必备 更多+
重装工具