sed命令实现文件内容替换总结案例

时间:2022-04-18 04:57:26

sed -i "s@AAAAA@BBBBB@g" /home/local/payment-biz-service/env/test.txt

sed -i "s#htxk.*#htxkAAAAA#g" /home/local/payment-biz-service/env/test.txt

案例使用:

项目中经常需要更改数据库链接地址,由于是多个项目同时更换

所以使用sed批量更改

将/home/local/payment-biz-service/env/目录下的db.propertiesbak文件中连接信息

sed命令实现文件内容替换总结案例

sed命令实现文件内容替换总结案例

jdbc.url=jdbc:mysql://192.168.10.101:3306/snsdb_test?allowMultiQueries=true

改成

jdbc.url=jdbc:mysql://192.168.10.71:3306/sportdb_test?allowMultiQueries=true

命令如下:

sed -i "s@mysql://192.168.10.101:3306/snsdb_test?allowMultiQueries=true@mysql://192.168.10.71:3306/sportdb_test?allowMultiQueries=true@g" /home/local/payment-biz-service/env/db.propertiesbak

sed命令实现文件内容替换总结案例

案例2:如果需要批量替换多个目录的db.properties文件内容呢?

分2步实现:

1.获取所有需要修改的db.properties文件列表

find / -type f -path '/home/local/*-service/env/db.*' -type f ! -path "*bak*" -type f ! -path "*db.properties_*"

sed命令实现文件内容替换总结案例

2.执行批量修改命令

find / -type f -path '/home/local/*-service/env/db.*' -type f ! -path "*bak*" -type f ! -path "*db.properties_*" | xargs sed -i "s@mysql://192.168.10.101:3306/snsdb_test?allowMultiQueries=true@mysql://192.168.10.71:3306/snsdb_test?allowMultiQueries=true@g"

sed命令实现文件内容替换总结案例

http://blog.csdn.net/devwang_com/article/details/52457591

http://www.jb51.net/article/59204.htm

http://www.jb51.net/article/110593.htm

http://blog.csdn.net/lizhi200404520/article/details/7968483

http://www.cnblogs.com/hnrainll/archive/2012/01/08/2316622.html

http://blog.csdn.net/wangmuming/article/details/19605281

http://blog.csdn.net/0210/article/details/44134383

http://blog.csdn.net/ocean20/article/details/7498198

http://www.cnblogs.com/ikodota/p/3797645.html

http://blog.sina.com.cn/s/blog_6419abc70100wi24.html

http://www.cnblogs.com/ctaixw/p/5860221.html

http://www.cnblogs.com/emanlee/archive/2013/09/07/3307642.html

http://blog.csdn.net/hua_ed/article/details/50402212

http://blog.csdn.net/jiary5201314/article/details/15339829

http://blog.csdn.net/hua_ed/article/details/50402212

http://blog.csdn.net/jkeven/article/details/8189966