'Trying to match and modify part of a line in awk or sed

I tried several sintaxes (sed/awk) to add a suffix at the end of specific lines that contains "ltm virtual /Common/" before "{" (its a bigip platform > bigip.conf) but Im not expert using it..

I got these lines at the file:

ltm virtual /Common/VS_exemple_80 {

ltm virtual /Common/VS_exemple_443 {

ltm virtual /Common/VS_test_80 {

ltm virtual /Common/VS_test_443 {

I do need to add XX at the end, like this:

ltm virtual /Common/VS_exemple_80_XX {

ltm virtual /Common/VS_exemple_443_XX {

ltm virtual /Common/VS_test_80_XX {

ltm virtual /Common/VS_test_443_XX {

Note that all lines that I need to perform this change starts with:

ltm virtual /Common/VS_XXXXX

What I tried to do is found the line "ltm virtual", run from VS_ to the end of the line "{" and append "_XX"

Thank you guys..

I tried:

sed -i '/^ltm virtual/s/[^ ]*$/&_XX/' bigip.conf.bkp

I can match the correct lines, but the suffix is append at the end of line..

ltm virtual /Common/VS_test_80 {_XX


Solution 1:[1]

You are matching the entire line, and then adding _XX at the end. You need to only match part of the line up to the {.

sed -r 's/^ltm virtual \/Common\/VS_\w+/&_XX/'

Note that the \w+ (one or more word characters) requires extended regular expressions, since you need the -r.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andy Lester