在多个文件中查找和替换特定术语 [英] Find and replace a particular term in multiple files

查看:24
本文介绍了在多个文件中查找和替换特定术语的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 Linux 中替换多个文件中的特定术语?

How can I replace a particular term in multiple files in Linux?

例如,我的目录中有许多文件:

For instance, I have a number of files in my directory:

file1.txt文件2.txtfile3.txt

file1.txt file2.txt file3.txt

我需要找到一个词searchword"并将其替换为replaceword".

And I need to find a word "searchword" and replace it with "replaceword".

推荐答案

sed -i.bak 's/searchword/replaceword/g' file*.txt
# Or sed -i.bak '/searchword/s/searchword/replaceword/g' file*.txt

使用 bash 4.0,您可以递归搜索文件

With bash 4.0, you can do recursive search for files

#!/bin/bash
shopt -s globstar
for file in **/file*.txt
do 
  sed -i.bak 's/searchword/replaceword/g' $file
  # or sed -i.bak '/searchword/s/searchword/replaceword/g' $file
done

或者用 GNU 查找

find /path -type f -iname "file*.txt" -exec sed -i.bak 's/searchword/replace/g' "{}" +;

这篇关于在多个文件中查找和替换特定术语的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆