FireHawk2300
FireHawk2300

Reputation: 97

How to find a match in file and then suffix that match

I have a file "test.xml" That looks like the below:

CLASS="BAKERS"
GROUP="ABCYYYYY"
TRACK="DASD"
OWNERS="ALPHA"
GROUP="ABCXXXXX"
GROUP="ABCZZZZZ"

I want to use a single SED line command to find all occurrences of GROUP="ABC Then within the "" I want add suffix: _DONE to all the matches found.

So the result should look like:

CLASS="BAKERS"
GROUP="ABCYYYYY_DONE"
TRACK="DASD"
OWNERS="ALPHA"
GROUP="ABCXXXXX_DONE"
GROUP="ABCZZZZZ_DONE"

This is the command I am using:

`sed -i.bkp '/^GROUP="ABC/ s/$/_DONE"/' test.xml`

but it is appending after the " and not within the ""

Upvotes: 2

Views: 258

Answers (5)

Eduardo Ramos
Eduardo Ramos

Reputation: 345

Use this:

sed  -i.bkp 's/GROUP="ABC[A-Z]*/&_DONE/g' test.xml

I tested with your example and worked.

Upvotes: 0

Kalanidhi
Kalanidhi

Reputation: 5092

You can use this sed command

sed '/GROUP="ABC/s/\(.*\)"/\1_DONE"/'

Output :

CLASS="BAKERS"
GROUP="ABCYYYYY_DONE"
TRACK="DASD"
OWNERS="ALPHA"
GROUP="ABCXXXXX_DONE"
GROUP="ABCZZZZZ_DONE"

Upvotes: 1

Gilles Quénot
Gilles Quénot

Reputation: 185254

Try doing this but without the backticks :

sed -i.bak '/^GROUP="ABC/s/"$/_DONE"/' file

Upvotes: 1

Kent
Kent

Reputation: 195109

this may help:

sed -i 's/^GROUP="ABC[^"]*/&_DONE/' file

Upvotes: 1

fredtantini
fredtantini

Reputation: 16556

It's almost that. But $ means end of line so you have to substitute the last " using "$ instead:

sed -i.bkp '/^GROUP="ABC/ s/"$/_DONE"/' test.xml

you could also specify that there is some blank after the " with for instance "[ \t]*$

Upvotes: 2

Related Questions