简体   繁体   English

搜索某些特定的字符串后,如何使用grep和sed替换子字符串?

[英]How to use grep and sed in order to replace the substring after searching some specific string?

I want to know how to use two 'grep' and 'sed' utilities or something else in order to replace the substring. 我想知道如何使用两个'grep'和'sed'实用程序或其他东西来替换子字符串。 I will explain what I want to do below. 我将在下面解释我想做什么。

We have the file 'test.txt' with the following string: 我们具有以下字符串的文件“ test.txt”:

A1='AA1', A2='AA2', A3='AA3', A4='AA4', A5{ATTR}='AA5', A6='keyword_A' A1 ='AA1',A2 ='AA2',A3 ='AA3',A4 ='AA4',A5 {ATTR} ='AA5',A6 ='keyword_A'

After searching 'keyword_A' using grep, I want to replace the value of A5 with other string, for example, "NEW". 使用grep搜索'keyword_A'之后,我想用其他字符串(例如“ NEW”)替换A5的值。

A1='AA1', A2='AA2', A3='AA3', A4='AA4', A5{ATTR}='NEW', A6='keyword_A' A1 ='AA1',A2 ='AA2',A3 ='AA3',A4 ='AA4',A5 {ATTR} ='NEW',A6 ='keyword_A'

I tried to use two commands like 我尝试使用两个命令,例如

grep keyword_A test.txt | sed -e 's/blabla/blabla/'

After trying all I know, I gave up at all. 在尝试了我所知道的一切之后,我完全放弃了。

Please let me know the right solution. 请让我知道正确的解决方案。

First, you never need grep and sed. 首先,您不需要grep sed。 Sed has a full regular-expression search engine, so it is a superset of grep. Sed具有完整的正则表达式搜索引擎,因此它是grep的超集。 This command will read test.txt , change the lines that you've indicated, and print the entire result on standard output: 该命令将读取test.txt ,更改您指示的行,并将整个结果打印在标准输出上:

sed "/keyword_A/s/A5{ATTR}='[A-Z0-9]*'/A5{ATTR}='NEW'/g" < test.txt

If you want to store the results back into the file test.txt , use the -i (in-place editing) switch to sed : 如果要将结果存储回文件test.txt ,请使用-i (就地编辑)开关切换到sed

sed "/keyword_A/s/A5{ATTR}='[A-Z0-9]*'/A5{ATTR}='NEW'/g" -i.bak test.txt

If you want to select only the indicated lines, modify those, and print only those lines to standard out, use a combination of the p (print) command and the -n (no output) switch. 如果要选择所指示的线,修改的那些,并且仅打印那些行到标准输出,则使用的组合p (印刷)命令和-n (无输出)开关。

sed "/keyword_A/s/A5{ATTR}='[A-Z0-9]*'/A5{ATTR}='NEW'/gp" -n test.txt

Using grep+sed is always the wrong approach. 使用grep + sed总是错误的方法。 Here's one way to do it with GNU awk: 这是使用GNU awk的一种方法:

$ awk '/keyword_A/{ $0=gensub(/(A5({[^}]+})?=\047)[^\047]+/,"\\1NEW",1) } 1' file
A1='AA1', A2='AA2', A3='AA3', A4='AA4', A5{ATTR}='NEW', A6='keyword_A'

Using a couple variables you could define the keyword and replacement ( if they change at all ): 使用几个变量,您可以定义关键字和替换(如果它们完全改变了):

q="keyword_A"
r="NEW"

Then with sed : 然后用sed

sed -r "s/^(.+\{.+\}=')(.+)('.+"${q}".+)$/\1"${r}"\3/" file

Result: 结果:

A1='AA1', A2='AA2', A3='AA3', A4='AA4', A5{ATTR}='NEW', A6='keyword_A'
A5="NEW"
A6="keyword_A"

# with sed
sed "s/='[^']*\(',[[:blank:]]*A6='${A6}'\)/='${A5}\1/" YourFile

# with awk
awk -F "'" -v A5="${A5}" -v A6="${A6}" '
   BEGIN { OFS="\047" }
   $12 == A6 { $10 = A5; $0 = $0 }
   7
   ' YourFile
  • Change by the end of the string, for sed and using ' as field separator in awk instead of traditional space. 在字符串末尾进行更改,以使用sed并在awk(而不是传统空间)中使用'作为字段分隔符”。
  • assuming there is no ' in value (or need to treat the escaping method) for awk version 假设不存在'在值(或需要治疗的逸出方法),用于AWK版本

找到字符串sting keyword_A时,我们可以直接替换第五列,如下所示:

awk -F, 'BEGIN{OFS=",";}/keyword_A/{$5="A5{ATTR}='"'"NEW"'"'"}1' filename

Couple of slight alternatives: 几个轻微的选择:

sed -r "/keyword_A/s/(A5[^']*')[^']*/\1NEW/"

awk -F"'" '/keyword_A/{$10 = "NEW"}1' OFS="'"

Of course the negative with awk is afterwards you would have to rename the new file. 当然,awk的负数是之后,您将不得不重命名新文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM