查找多文件内容替换

本文介绍了一种在Linux环境下利用find结合sed命令实现跨多个文件查找并替换特定文本的方法。此方法适用于系统管理员或开发者需要快速修改大量文件中的指定字符串的情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[color=red]UPDATE: (05/03/2013)[/color]
当前使用:


#去掉所有的FactoryGirl.
find . -type f -name *_spec.rb -print | xargs sed -ie 's/FactoryGirl\.//g'




//Seems like Linux is being far too generous.

-i 's/^[[:space:]]\{11\}//g' <-- makes it look like
"s/^[[:space:]]\{11\}//g" is the extension given to "-i"

//baf's quotes serve to clarify to the interpreter (and to humans reading it) that
there is no extension. hayne's -e is also a means with which to disambiguate.

//Thus, these all work:
sed -i "" 's/^[[:space:]]\{11\}//g' menu1a.sh
sed -i -e 's/^[[:space:]]\{11\}//g' menu1a.sh
sed -ie 's/^[[:space:]]\{11\}//g' menu1a.sh
sed -i "" -e 's/^[[:space:]]\{11\}//g' menu1a.sh

Makes sense.



find . -name "*.php" -print | xargs sed -i 's/foo/bar/g'

$ find /home/bruno/old-friends -type f -exec sed -i 's/ugly/beautiful/g' {} \;



[quote]

ere’s the post on my new blog – http://rushi.vishavadia.com/blog/find-replace-across-multiple-files-in-linux/
Below is an older version of the post:

I was trying to find a solution todo a find & replace across multiple files which was purely command line based. There are plenty of scripts out there which will accomplish this but I needed a single line command. After some google searches and some experimentation I came up with this snippet.

find . -name "*.php" -print | xargs sed -i 's/foo/bar/g'

It looks a bit complicated but its quite simple. There are three components to the command:

find . -name "*.php" -print – Find all files (recursively) which has “.php” in the file and print them out. This will give you output like this:

./file.php
./includes/test.php
./classes/class.php

xargs- This command is used when you want to pass a lot of arguments to one command. xargs will combine the single line output of find and run commands with multiple
arguments, multiple times if necessary to avoid the max chars per line limit. In this case we combine xargs with sed
sed -i 's/foo/bar/g' – aka Stream Editor is a tool which should be in every sys admin’s toolkit. In this case every occurence of “foor” is replaced by “bar” in all the files found using the “find” command. Sed simply parses input and applies certain text transformations to it. There’s a lot to say about sed, you can find more at this tutorial.

This pretty much covers the core of the find & replace command. You could also open up a particular folder in an IDE and use it’s find and replace feature. But find + sed is quite fast and powerful.
[/quote]
小软件Replace Plus:为多个文本文件做内容批量替换 当我们的文本里有些相同的文本内容替换成其他内容时,可以运用到“文本替换”功能,这一功能在诸多的文本编辑软件里都有,并不算稀奇。但是,如果你有多个文件,想要同时替换它们里面的一些相同内容,显然不是其简单的替换功能就能够达到的。要想批处理完成N个文件的文本同时替换,我给大家推荐一个小工具——Replace Plus。它是一款绿色小软件,并不需要安装,直接双击打开即可使用。   在列表栏里点击右键,选择“添加文件”即可将单独的文件添加进列表,如果您想要更加省事一些,就将欲替换文本的文件放置于一个文件夹下。右键点击选择“添加目录”,将那个文件夹添加进来,该目录下的所有文件就直接添加进来了,省去了一个一个添加的麻烦。   在“查找”栏内输入欲查找内容,在“替换”栏里输入欲替换内容,点击“执行替换”,默认情况下会跳出“替换”对话框询问是否要替换,如果要一劳永逸地不必每个文件都跳出对话框来询问,可直接点击“替换全部”。   当列表里的“备注”栏显示出“替换成功”,即表示已经将不同文件里的相同内容替换完成。并且在列表里还会显示替换成功的数量,一目了然(如图),很简便也很实用吧!要删除列表里的内容,直接选定欲删除的内容后,在右键菜单里点击“删除”即可。  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值