FireHawk2300 FireHawk2300 - 3 years ago 215
Linux Question

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

Answer Source

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]*$

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download