Regex Unix shell脚本:在“输入”时插入文本;“文本模式”;找到
假设我有两个文件: -data.txt -report.txt 报告包含带有一些“占位符”的文本,例如:Regex Unix shell脚本:在“输入”时插入文本;“文本模式”;找到,regex,shell,unix,cat,Regex,Shell,Unix,Cat,假设我有两个文件: -data.txt -report.txt 报告包含带有一些“占位符”的文本,例如: HPLC results for sample: <insert-sample-id-here>, elements analyzed: <insert-element-name> 这里不是计算机高手,但我知道“查找和替换”的任务可以由计算机自动完成;“用我的数据文件中匹配对的值替换占位符后,应使用何种命令重写report.txt?”在当前的文件格式中,很难将数据文
HPLC results for sample: <insert-sample-id-here>, elements analyzed: <insert-element-name>
这里不是计算机高手,但我知道“查找和替换”的任务可以由计算机自动完成;“用我的数据文件中匹配对的值替换占位符后,应使用何种命令重写report.txt?”在当前的文件格式中,很难将数据文件中的值与报告文件中的占位符匹配,因为占位符
与data.txt中的sampleID
不同。
与elementName
不相同
但假设您有不同形式的data.txt
:
insert-sample-id-here: 123456abc
insert-element-name: genericThickeningAgent
(因此,如果data.txt
中的键与report.txt
中的占位符匹配),可以使用以下命令:
$ perl -ne 'print "s/<$1>/$2/g\n" if /^(.*?):\s+(.*)$/' data.txt | \
xargs -I% perl -p -i -w -e "%" report.txt
哇,这真是超出了我的要求!我认为有必要为每一对写下“查找和替换”。我看看是否可以更改报告模板。非常感谢您的解决方案,太好了!
$ perl -ne 'print "s/<$1>/$2/g\n" if /^(.*?):\s+(.*)$/' data.txt | \
xargs -I% perl -p -i -w -e "%" report.txt
$ cat report.txt
results for sample: 123456abc, elements analyzed: genericThickeningAgent