[英]bash replace string in file with specific index
I have 2 files: 我有2个文件:
idfile.txt
: idfile.txt
:
1111
3333
replace.xml
: replace.xml
:
<condition="online" id="1111" >
<condition="online" id="2222" >
<condition="online" id="3333" >
<condition="online" id="4444" >
I need a script to get below output.xml
: 我需要一个脚本来获取以下
output.xml
:
<condition="offline" id="1111" >
<condition="online" id="2222" >
<condition="offline" id="3333" >
<condition="online" id="4444" >
I use: 我用:
while read line; do
grep $line replace.xml | sed 's/condition="online"/condition="offline"/g' replace.xml >> output.xml
done < idfile.txt
My script replace all condition="online"
in condition="offline"
. 我的脚本替换了
condition="offline"
所有condition="online"
condition="offline"
。
Thanks a lot! 非常感谢!
Note, id
attribute value from replace.xml
should match any of the entries from idfile.txt
to fit the condition. 注意,
id
从属性值replace.xml
应匹配任何从条目idfile.txt
适合的条件。
Awk + paste solution: AWK + 粘贴解决方案:
awk -v ids="$(paste -s -d'|' idfile.txt)" 'match($2,ids){ sub("online","offline",$1) }1' replace.xml
The output: 输出:
<condition="offline" id="1111" >
<condition="online" id="2222" >
<condition="offline" id="3333" >
<condition="offline" id="4444" >
If your files are going to be large, I would use awk
. 如果您的文件很大,我将使用
awk
。 Please note that your idfile.txt
should be: 请注意,您的
idfile.txt
应该为:
1111
3333
To get something interesting. 得到一些有趣的东西。
This is how I would do it with awk
: 这就是我使用
awk
:
#!/bin/bash
awk '
BEGIN {
while( (getline $l < "idfile.txt") > 0 ) {
if( $l ~ /^.+$/ ) {
id[$l] = 1;
}
}
close("idfile.txt");
}
/^.+$/ {
split($2, a, "\"");
if( id[ a[2] ] ) {
printf "<condition=\"online\" id=\"%s\">\n", a[2];
}
else {
printf "<condition=\"offline\" id=\"%s\">\n", a[2];
}
}
' replace.xml >output.xml
The BEGIN
block reads the id file to the id
array. BEGIN
块将id文件读取到id
数组。 awk
uses a hash to implement lookups so they are efficient. awk
使用散列来实现查找,因此它们是有效的。 The regular expressions /^.+$/
are meant to avoid processing empty lines. 正则表达式
/^.+$/
旨在避免处理空行。 The code is meant to be contained in a bash
(text) file. 该代码应包含在
bash
(文本)文件中。
$2
will get the pieces id="nnnn">
and the split
will get in array a[2]
the part of that within the quotes. $2
将得到片段id="nnnn">
,并且split
将在数组a[2]
中得到引号内的部分。
awk one liner awk一线
$ awk 'FNR==NR{a[$0]; next} ($4 in a){gsub(/online/,"offline")}1' idfile.txt FS='"' replace.xml
First
: Store all id's in a
First
:存储所有的ID在a
Next
, while iterating over replace.xml, if id ie $4
exists in array a
then replace online
with offline
. Next
,在遍历replace.xml时,如果id即$4
存在于数组a
则将online
替换为offline
。 Note: Field separator is "
for replace.xml 注意:对于replace.xml,字段分隔符为
"
Output 产量
<condition="offline" id="1111" >
<condition="online" id="2222" >
<condition="offline" id="3333" >
<condition="online" id="4444" >
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.