shell脚本 - 搜索和使用字符串列表替换多个文件中的文本 [英] Shell script - search and replace text in multiple files using a list of strings

查看:469
本文介绍了shell脚本 - 搜索和使用字符串列表替换多个文件中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件changesDictionary.txt含(可变数量)对键 - 值的字符串。

I have a file "changesDictionary.txt" containing (a variable number of) pairs of key-value strings.

例如

textToSearchFor=theReplacementText

"textToSearchFor" = "theReplacementText"

(字典的格式是不重要,并且根据需要改变。)

(The format of the dictionary is unimportant, and be changed as required.)

我需要通过给定目录的内容,包括子目录进行迭代。对于具有扩展名.txt中遇到的每个文件,我们搜索每个在changesDictionary.txt的按键,用替换字符串值替换每个发现的实例。

I need to iterate through the contents of a given directory, including sub-directories. For each file encountered with the extension ".txt", we search for each of the keys in changesDictionary.txt, replacing each found instance with the replacement string value.

即。一个搜索和替换在多个文件,但使用搜索的列表/替换方面,而不是一个单一的搜索/替换术语

i.e. a search and replace over multiple files, but using a list of search/replace terms rather than a single search/replace term.

我怎么能这样做呢? (我研究过单一的搜索/替换的例子,但不知道如何在一个文件中做多次搜索。)

How could I do this? (I have studied single search/replace examples, but do not understand how to do multiple searches within a file.)

执行(bash的,perl的,等等)并不重要,只要我可以在Mac OS X由于在命令行中的任何帮助运行它。

The implementation (bash, perl, whatever) is not important as long as I can run it from the command line in Mac OS X. Thanks for any help.

推荐答案

我想你changesDictionary.txt文件转换为sed脚本,有... sed的:

I'd convert your changesDictionary.txt file to a sed script, with... sed:

$ sed -e 's/^"\(.*\)" = "\(.*\)"$/s\/\1\/\2\/g/' \
      changesDictionary.txt  > changesDictionary.sed

注意下,对于任何一个正规的前$ P $在你的字典pssions或sed的前pressions任何特殊字符将是中美战略经济对话PTED虚假间$ P $,所以你的字典既可以只只有最原始的搜索和替换,或者你需要保持与前有效pressions sed的文件。不幸的是,有没有简单的方法在sed要么关闭前定期pression,只使用字符串匹配或引用您的搜索和替换为文本。

Note, any special characters for either regular expressions or sed expressions in your dictionary will be falsely interpreted by sed, so your dictionary can either only have only the most primitive search-and-replacements, or you'll need to maintain the sed file with valid expressions. Unfortunately, there's no easy way in sed to either shut off regular expression and use only string matching or quote your searches and replacements as "literals".

通过生成的sed脚本,使用find的的xargs的 - 而不是找到-exec - 将文件尽快转化与sed脚本,通过处理他们一比一处时间。

With the resulting sed script, use find and xargs -- rather than find -exec -- to convert your files with the sed script as quickly as possible, by processing them more than one at a time.

$ find somedir -type f -print0 \
   | xargs -0 sed -i -f changesDictionary.sed

注意的SED编辑文件 -i 选项就地,所以一定要进行备份的安全性,或使用 -i〜以创建波浪的备份。

Note, the -i option of sed edits files "in-place", so be sure to make backups for safety, or use -i~ to create tilde-backups.

最后说明后,使用搜索和替换可以有意想不到的后果。你将有是其他搜索子搜索?下面是一个例子。

Final note, using search and replaces can have unintended consequences. Will you have searches that are substrings of other searches? Here's an example.

$ echo '"fix" = "broken"' > changesDictionary.txt
$ echo '"fixThat" = "Fixed"' >> changesDictionary.txt
$ sed -e 's/^"\(.*\)" = "\(.*\)"$/s\/\1\/\2\/g/' changesDictionary.txt  \
   | tee changesDictionary.sed
s/fix/broken/g
s/fixThat/Fixed/g
$ mkdir subdir
$ echo fixThat > subdir/target.txt
$ find subdir -type f -name '*.txt' -print0 \
   | xargs -0 sed -i -f changesDictionary.sed
$ cat subdir/target.txt
brokenThat

应该fixThat,已经成为固定或brokenThat?订购changesDictionary.sed事宜。同样,搜索和替换可以搜索和替换不止一次 - 变a到B,可以通过改变另一个搜索和替换从B至C

Should "fixThat" have become "Fixed" or "brokenThat"? Order matters for changesDictionary.sed. Similarly, a search and replace can be search and replaced more than once -- changing "a" to "b", may be changed by another search-and-replace from "b" to "c".

也许你已经考虑这两个,但我提到,因为我已经试过你做之前并没有想到这一点。我不知道的东西,根本的做正确的事的的同时做多个搜索和替换。所以,你需要给它编程,自己做了正确的事情。 (也许是其他岗位#1存在?)

Perhaps you've already considered both of these, but I mention because I've tried what you were doing before and didn't think of it. I don't know of anything that simply does the right thing for doing multiple search and replacements at once. So, you need to program it to do the right thing yourself. (Perhaps another Stackoverflow post exists?)

过去和最后要注意,我不使用Mac OS X,但shell命令我的GNU / Linux机器上运行。我不知道你的经验是用找到的xargs SED 是的,但我记得Mac OS X中有旧的或不同的生成的这些工具。所以,你可能能够做到这一点,但它可能需要小的修改。

Last and final note, I don't use Mac OS X, but the shell commands work on my GNU/Linux machine. I'm not sure what your experience is with find, xargs or sed is, but I recall Mac OS X had older or different builds of these tools. So, you may be able to accomplish this, but it may need small modification.

这篇关于shell脚本 - 搜索和使用字符串列表替换多个文件中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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