linux --批量修改文件内容

拟墨画扇 提交于 2019-11-29 06:22:17

由于目前测试的BIOS有一个option 发生了改变,因此我们需要在之前写好的脚本上进行修改,将旧的option 改为新的选项,因此在此处用到了批量修改文件中的内容;

1. perl 命令替换:

perl -i -e "s/old/new/g" the path of the file

下面,就将test1 text2,中的cat 都换成了dog,汪汪~~

[root@11 tmp]# touch test1.txt
[root@11 tmp]# vim test1.txt
[root@11 tmp]# cat test1.txt
cat cat cat
i like linux
i like windows too
i like watching TV
I like chenqingling

[root@11 tmp]# cp test1.txt text2.txt
[root@11 tmp]# cp test1.txt text3.txt
[root@11 tmp]# cp test1.txt text4.txt
[root@11 tmp]# perl -p -i -e "s/cat/dog/g" test1.txt text2.txt
[root@11 tmp]# cat test1.txt
dog dog dog
i like linux
i like windows too
i like watching TV
I like chenqingling

[root@11 tmp]# cat text2.txt
dog dog dog
i like linux
i like windows too
i like watching TV
I like chenqingling

[root@11 tmp]# cat text3.txt
cat cat cat
i like linux
i like windows too
i like watching TV
I like chenqingling

2.运用sed 命令批量修改文件内容:

sed -i "s/old/new/g" the path of the files

修改text2,text3,中的linux ,修改为Unix

[root@11 tmp]# sed -i "s/linux/Unix/g" text2.txt text3.txt
[root@11 tmp]# cat text2.txt
dog dog dog
i like Unix
i like windows too
i like watching TV
I like chenqingling

[root@11 tmp]# cat text3.txt
cat cat cat
i like Unix
i like windows too
i like watching TV
I like chenqingling

[root@11 tmp]# cat test1.txt
dog dog dog
i like linux
i like windows too
i like watching TV
I like chenqingling

 3.补充点:将old 全部换成 new 

sed -i “s/old/new/g” `grep old -rl /path`    #当前路径表示:./

[root@110 lu]# sed -i "s/TV/xiaozhan/g" `grep TV -rl ./`
[root@11 lu]# cat text2.txt
dog dog dog
i like Unix
i like windows too
i like watching xiaozhan
I like chenqingling

[root@11 lu]# cat text3.txt
cat cat cat
i like Unix
i like windows too
i like watching xiaozhan
I like chenqingling

[root@11 lu]# cat text4.txt
cat cat cat
i like linux
i like windows too
i like watching xiaozhan
I like chenqingling

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!