Linux在所有文件中查找和替换
来源:互联网
时间:2026-02-26 13:35:40
作者:
系统大全为您提供
经常会碰到这样的情况:查找某个目录下所有包含某个字符串的所有文件,并将这些文件中的这个字符串用另外的字符串替换进行替换。这种情况下,网网要检查的文件比较多,逐一进行检查替换太麻烦,这个时候,我们就应该找一个能够一条命令解决问题的方法。 1、grep命令
grep pattern file.txt命令默认的行为是将file.txt文件中,匹配pattern的行输出到标准输出。这个功能能帮助我们在文件中查找一个字符串出现的上下文,但是并不能够帮助我们实现下一步复杂的操作,所以有必要稍微了解下grep的一些选项。
现在测试文件的内容如下:
$ cat target.txt
<a class="navbar-brand" href="https://www.herecours.com/d/file/efpub/2026/26-26/20260226130945568911 text-decoration: none;" href="https://www.herecours.com/d/file/efpub/2026/26-26/20260226130945568912.html">Xindongbr " href="https://www.herecours.com/d/file/efpub/2026/26-26/20260226130945568913
<a class="navbar-brand" href="https://www.herecours.com/d/file/efpub/2026/26-26/20260226130945568911
href="https://www.herecours.com/d/file/efpub/2026/26-26/20260226130945568914.html">br
href="https://www.herecours.com/d/file/efpub/2026/26-26/20260226130945568915 为了测试我们将这个文件,另外拷贝出两份,重命名然后放在如下的目录结构中:
$ tree .
.
├── a
│ └── target1.txt
├── target2.txt
└── target.txt
执行grep命令:
$ grep -rn "https://www.herecours.com/d/file/efpub/2026/26-26/20260226130945568916.cn" br a/target1.txt:1:<a class="navbar-brand" href="https://www.herecours.com/d/file/efpub/2026/26-26/20260226130945568911 a/target1.txt:5: <a class="navbar-brand" href="https://www.herecours.com/d/file/efpub/2026/26-26/20260226130945568911 target2.txt:1:<a class="navbar-brand" href="https://www.herecours.com/d/file/efpub/2026/26-26/20260226130945568911 target2.txt:5: <a class="navbar-brand" href="https://www.herecours.com/d/file/efpub/2026/26-26/20260226130945568911 target.txt:1:<a class="navbar-brand" href="https://www.herecours.com/d/file/efpub/2026/26-26/20260226130945568911 target.txt:5: <a class="navbar-brand" href="https://www.herecours.com/d/file/efpub/2026/26-26/20260226130945568911 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 "https://www.herecours.com/d/file/efpub/2026/26-26/20260226130945568917.cn" )br $ cat target.txt
<a class="navbar-brand" href="/">Panda Search</a>
text-decoration: none;" href="https://www.herecours.com/d/file/efpub/2026/26-26/20260226130945568912.html">Xindongbr " href="https://www.herecours.com/d/file/efpub/2026/26-26/20260226130945568913
<a class="navbar-brand" href="/">Panda Search</a>
href="https://www.herecours.com/d/file/efpub/2026/26-26/20260226130945568914.html">br
href="https://www.herecours.com/d/file/efpub/2026/26-26/20260226130945568915 $ sed -i "stp://www.asdf.net//g" $(grep -lr "https://www.herecours.com/d/file/efpub/2026/26-26/20260226130945568918.net" )br $ sed -i "stp://asdf.net//g" $(grep -lr "https://www.herecours.com/d/file/efpub/2026/26-26/20260226130945568919.net" ) br $ 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
$
看到这里,或许你也明白我要做什么了。其实,是这样的,一位童鞋往我们线上的系统中加了几个静态网页,网页中用到的超链全部是直接从浏览器复制过来的,包括站内的跳转。这样明显不科学,尽管当时是没毛病,但是现在我们的域名要换了,问题就来了,这些老域名的跳转链接都不能访问了。为了根治这个问题,我决定将这些站内跳转的域名部分全部
以上就是系统大全给大家介绍的如何使的方法都有一定的了解了吧,好了,如果大家还想了解更多的资讯,那就赶紧点击系统大全官网吧。
本文来自系统大全https://www.herecours.com/d/file/efpub/2026/26-26/20260226130945568920
编辑推荐:
- Linux在所有文件中查找和替换02-26
- reboot命令--Linux命令应用大词典729个命令解读02-26
- 必看的100个常用linux命令02-26
- linux句柄泄露问题查看02-26
- Linux c 获取本机IP的方法02-26
- 每天一个Linux命令(32)gzip命令02-26
- Linux中的free命令02-26
- Linux下LVM的配置详解(2)02-26
下一篇:
相关推荐
-
雷神推出 MIX PRO II 迷你主机:基于 Ultra 200H,玻璃上盖 + ARGB 灯效
2 月 9 日消息,雷神 (THUNDEROBOT) 现已宣布推出基于英
-
制造商 Musnap 推出彩色墨水屏电纸书 Ocean C:支持手写笔、第三方安卓应用
2 月 10 日消息,制造商 Musnap 现已在海外推出一款 Oce
热文推荐
- Linux中的free命令
Linux中的free命令
26-02-26 - Linux系统怎么架设共享文件服务器?
Linux系统怎么架设共享文件服务器?
26-02-26 - Windows和linux双系统——修改默认启动顺序
Windows和linux双系统——修改默认启动顺序
26-02-26 - 三只羊网络回应网传“借壳上市成功”:不实信息
三只羊网络回应网传“借壳上市成功”:不实信息
26-02-26 - 携程集团:反垄断调查目前无法预计结果,公司业务运营保持正常
携程集团:反垄断调查目前无法预计结果,公司业务运营保持正常
26-02-26 - linux打开端口《图》
linux打开端口《图》
26-02-26 - Linux常见目录详解图
Linux常见目录详解图
26-02-26 - linux设备驱动程序的编写
linux设备驱动程序的编写
26-02-26 - Linux下的网络配置命令
Linux下的网络配置命令
26-02-26 - VNC远程连接Linux系统设置
VNC远程连接Linux系统设置
26-02-26
