批量更改多个文件内容

默北 Linux命令215,3607字数 356阅读1分11秒阅读模式

需要修改多个文件的内容,一个一个的替换比较繁琐。那么有没有什么好的方法直接多多个文件进行批量修改呢?方法如下:

1. sed

grep old_string -rl ./* | xargs sed -i "s/old_string/new_string/g"文章源自运维生存时间-https://www.ttlsa.com/linux-command/batch-change-multiple-file-content/

sed -i "s/old_string/new_string/g" `grep old_string -rl ./*`文章源自运维生存时间-https://www.ttlsa.com/linux-command/batch-change-multiple-file-content/

如:文章源自运维生存时间-https://www.ttlsa.com/linux-command/batch-change-multiple-file-content/

sed -i "s/11/xxx/g" `grep 11 -rl ./*`文章源自运维生存时间-https://www.ttlsa.com/linux-command/batch-change-multiple-file-content/

grep xxxx -rl ./* | xargs sed -i "s/x/asdfadsfasdfafd/g"文章源自运维生存时间-https://www.ttlsa.com/linux-command/batch-change-multiple-file-content/

2. perl

perl -pi -e 's|old_string|new_string|g' `find ./ -type f`文章源自运维生存时间-https://www.ttlsa.com/linux-command/batch-change-multiple-file-content/

如:文章源自运维生存时间-https://www.ttlsa.com/linux-command/batch-change-multiple-file-content/

perl -pi -e 's|ISO-8859-1|UTF-8|g' `find ./ -type f`文章源自运维生存时间-https://www.ttlsa.com/linux-command/batch-change-multiple-file-content/ 文章源自运维生存时间-https://www.ttlsa.com/linux-command/batch-change-multiple-file-content/

weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
默北
  • 本文由 发表于 05/12/2014 01:00:18
  • 转载请务必保留本文链接:https://www.ttlsa.com/linux-command/batch-change-multiple-file-content/
评论  2  访客  2
    • aiplaypc
      aiplaypc 0

      批量更新不多的文件还行,如果遇到上万个文件批量更新就不行了,会列表溢出吧,就应该用循环来解决了

        • 默北
          默北

          @ aiplaypc 情况多种多样,点明白了,面就清晰了

      评论已关闭!