SeungCheol Han
SeungCheol Han

Reputation: 125

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. I will explain what I want to do below.

We have the file 'test.txt' with the following string:

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".

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.

Upvotes: 1

Views: 128

Answers (6)

grail
grail

Reputation: 930

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.

Upvotes: 0

Vicky
Vicky

Reputation: 1328

We can just directly replace the fifth column when the sting keyword_A is found as shown below:

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

Upvotes: 0

NeronLeVelu
NeronLeVelu

Reputation: 10039

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.
  • assuming there is no ' in value (or need to treat the escaping method) for awk version

Upvotes: 0

l'L'l
l'L'l

Reputation: 47169

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 -r "s/^(.+\{.+\}=')(.+)('.+"${q}".+)$/\1"${r}"\3/" file

Result:

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

Upvotes: 0

Ed Morton
Ed Morton

Reputation: 203502

Using grep+sed is always the wrong approach. Here's one way to do it with 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'

Upvotes: 0

Robᵩ
Robᵩ

Reputation: 168626

First, you never need grep and sed. Sed has a full regular-expression search engine, so it is a superset of grep. This command will read test.txt, change the lines that you've indicated, and print the entire result on standard output:

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:

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.

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

Upvotes: 2

Related Questions