Friday, April 15, 2022

[SOLVED] Trying to match and modify part of a line in awk or sed

Issue

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

The $ at the end of your regexp is anchoring it to the end of the line and therefore making it not match any string in the input, plus you're trying to use /s in a regexp that's delimited by /s and you can't do that - when trying to match the regexp a/b in sed, instead of /a/b/ you have to escape the / like /a\/b/ or more concisely just use a different delimiter, e.g. :a/b:.

Using any sed:

$ sed 's:ltm virtual /Common/[^ ]*:&_XX:' file
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 {


Answered By - Ed Morton
Answer Checked By - Terry (WPSolving Volunteer)