简体   繁体   English

bash用特定索引替换文件中的字符串

[英]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.

相关问题 使用 bash 文件替换字符串 - Replace a string using bash file 仅当包含它的行不包含使用 Bash 的特定字符串时,如何替换文件中的字符串? - How to replace a string in a file only if the line containing it does not also contain a specific string using Bash? Bash复制文件并替换文件名中的字符串 - Bash copy file and replace string in file name 如何使用行号 bash 脚本将文件中的特定行替换为字符串变量? - How to replace a specific line in a file with a string variable using the line number bash script? 使用bash,如何查找所有包含特定字符串的文件并将其替换为现有文件? - Using bash, how do I find all files containing a specific string and replace them with an existing file? 用搜索字符串中的/替换bash文件中的文本 - Replace text in a file in bash with / in the search string 从 bash 脚本替换文件中的字符串 - Replace string within a file from a bash script 从BASH中的另一个文件中的字符串替换一个文件中的字符串 - replace string in one file from string in other file in BASH 在文件中找到单词的索引并用 bash 替换以前的字符 - find index of word in file and replace previous characters with bash 使用 bash 脚本将包含字符串的前两行替换为特定行 - Replace the first 2 lines containing a string with a specific line with a bash script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM