用于在文件中查找、搜索和替换字符串数组的 Shell 脚本 [英] Shell script to find, search and replace array of strings in a file
问题描述
这与我在 代码高尔夫上问的另一个问题/代码高尔夫相关联:颜色突出显示"重复文本
我有一个包含以下内容的文件sample1.txt":
I've got a file 'sample1.txt' with the following content:
LoremIpsumissimplydummytextoftheprintingandtypesettingindustry.LoremIpsumhasbeentheindustry'sstandarddummytexteversincethe1500s,whenanunknownprintertookagalleyoftypeandscrambledittomakeatypespecimenbook.
我有一个脚本,可以生成文件中出现的以下字符串数组(仅显示了一些用于说明):
I've got a script generating the following array of strings which occur in the file (only a few shown for illustration):
LoremIpsum
LoremIpsu
dummytext
oremIpsum
LoremIps
dummytex
industry
oremIpsu
remIpsum
ummytext
LoremIp
dummyte
emIpsum
industr
mmytext
我需要(从顶部)查看文件 sample1.txt 中是否出现LoremIpsum".如果是这样,我想将所有出现的 LoremIpsum 替换为:
.现在,当程序移动到下一个单词LoremIpsu"时,它不应该与 sample1.txt 中的
文本匹配.它应该对这个数组"的所有元素重复上述操作.下一个有效"的将是dummytext",它应该被标记为
.
I need to (from the top) see if 'LoremIpsum' occurs in file sample1.txt. If so, I want to replace all occurences of LoremIpsum with: <T1>LoremIpsum</T1>
. Now, when the program moves to the next word 'LoremIpsu', it should NOT match against the <T1>LoremIpsum</T1>
text inside sample1.txt. It should repeat the above for all elements of this 'array'. The next 'valid' one would be 'dummytext' and that should be tagged as <T2>dummytext</T2>
.
我确实认为应该可以为此创建一个 bash shell 脚本解决方案,而不是依赖 perl/python/ruby 程序.
I do think it should be possible to create a bash shell script solution for this rather than relying on perl/python/ruby programs.
推荐答案
Pure Bash (no externals)
Pure Bash (no externals)
在 Bash 命令行:
At the Bash command line:
$ sample="LoremIpsumissimplydummytextoftheprintingandtypesettingindustry.LoremIpsumhasbeentheindustry'sstandarddummytexteversincethe1500s,whenanunknownprintertookagalleyoftypeandscrambledittomakeatypespecimenbook."
$ # or: sample=$(<sample1.txt)
$ array=(
LoremIpsum
LoremIpsu
dummytext
...
)
$ tag=0; for entry in ${array[@]}; do test="<[^>/]*>[^>]*$entry[^<]*</"; if [[ ! $sample =~ $test ]]; then ((tag++)); sample=${sample//${entry}/<T$tag>$entry</T$tag>}; fi; done; echo "Output:"; echo $sample
Output:
<T1>LoremIpsum</T1>issimply<T2>dummytext</T2>oftheprintingandtypesetting<T3>industry</T3>.<T1>LoremIpsum</T1>hasbeenthe<T3>industry</T3>'sstandard<T2>dummytext</T2>eversincethe1500s,whenanunknownprintertookagalleyoftypeandscrambledittomakeatypespecimenbook.
这篇关于用于在文件中查找、搜索和替换字符串数组的 Shell 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!