简体   繁体   English

如何从命令行进行递归查找和替换?

[英]How to do a recursive find and replace from the command line?

Is there a simple way to recursively search all files in a directory hierarchy for a term (eg port ) and replace all occurrences of that term with another (eg port-lookup ). 是否有一种简单的方法可以递归搜索目录层次结构中的所有文件(例如port ),并将该术语的所有匹配项替换为另一个(例如port-lookup )。

I have tried the suggestions in this post , but they are only applicable for a single file. 我已尝试过这篇文章中的建议,但它们仅适用于单个文件。

Try this 尝试这个

find ./ -type f -exec sed -i -e 's/port/port-lookup/g' {} \;

If you don't want to change file modification time 如果您不想更改文件修改时间

grep -rl 'port' /search_in_this_dir | xargs sed -i 's/port/port-offset/g' 

The simplest (and arguably most pure and intuitive) way to achieve this is to perform a recursive file grep then pipe those file results to sed (via xargs ) to handle the in-place substitution. 实现此目的的最简单(也可以说是最纯粹和最直观)的方法是执行递归文件grep然后将这些文件结果传递给sed(通过xargs )来处理就地替换。

grep -r -l "port" | xargs -r -d'\n' sed -i 's/port/port-lookup/g'

There is a comprehensive community wiki answer on this topic that I would recommend reading over on the Unix & Linux exchange. 关于这个主题有一个全面的社区wiki答案 ,我建议你阅读Unix和Linux交换。

If you have a recent version of bash (>4) 如果您有最新版本的bash(> 4)

shopt -s globstar
sed -i 's/port/port-lookup/g' **/*

Just 1 command line: msr -rp dir1,dir2,file1,fileN -x port -o port-lookup -R 只需1个命令行: msr -rp dir1,dir2,file1,fileN -x port -o port-lookup -R

The above command recusively search and replaces plain text port to port-lookup in files. 上面的命令重复搜索并将纯文本port替换为文件中的port-lookup

  • If you want to preview replacing result , remove -R 如果要预览替换结果 ,请删除-R
  • If you want to preview and summarize files list and count/distribution, add -l 如果要预览和汇总文件列表和计数/分布,请添加-l
  • If you want to backup the changed files, add -K like -K -R or -RK . 如果要备份更改的文件,请添加-K-K -R-RK
  • Ignore case add -i like -i -x or -ix . 忽略大小写添加-i-i -x-ix
  • If you want to use C++ / C# / Java / Scala Regex syntax, use -t instead of -x . 如果要使用C++ / C# / Java / Scala Regex语法,请使用-t而不是-x
  • Filter file name like: include -f "\\.(xml|java|cpp)$" , exclude --nf "\\.(so|a|lib|dll|obj)$" 过滤文件名如:include -f "\\.(xml|java|cpp)$" ,exclude --nf "\\.(so|a|lib|dll|obj)$"
  • Filter directory name like: include -d "src" , exclude --nd "^(target|\\.git)$" 过滤目录名称如:include -d "src" ,exclude --nd "^(target|\\.git)$"
  • Filter file size range like: --s1 1 / --s1 1B with/or --s2 1.20MB 过滤文件大小范围如: - --s1 1 / --s1 1B with /或--s2 1.20MB
  • Filter file last write time range like: --w1 2017-08-01 with/or --w2 "2017-08-12 11:20:30" 过滤文件的最后写入时间范围如下: - --w1 2017-08-01 with /或--w2 "2017-08-12 11:20:30"

More usages about find and replace file or pipe just see my open project https://github.com/qualiu/msr ( msr.exe / msr.gcc* / msr.cygwin are in the tools directory). 关于查找和替换文件或管道更多的用途只是看到我打开的项目https://github.com/qualiu/msrmsr.exe / msr.gcc* / msr.cygwin是在tools目录)。 Built-in usage and exmaples just run the exe. 内置使用和exmaples只需运行exe。 Docs like performance comparision with findstr and grep . Docs findstrgrep的性能比较

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM