linux --批量修改文件内容
由于目前测试的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
每天进步一点点~~
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南