Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

linux怎么批量修改文件内容?

linux怎么批量修改文件内容?

比如:
操作系统是centos7,在两个目录/var/www/example1/var/www/example2有两个相同的文件student.txt,内容如下:

shool:No.1 School
class:class 3
grade:grade 2
name:test

我想把两个目录中的这个student.txt文件的第4行的test分别改成ZhangSanLiSi
我想写个shell脚本modify-name.sh来完成这个功能,可是不知道该怎么写,请大神帮写一下,谢谢!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

不必写脚本,用两条命令就可以解决了。如:
sed -i '/name/s/test/ZhangSan/' /var/www/example1/student.txt
sed -i '/name/s/test/LiSi/' /var/www/example2/student.txt


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...