Home > front end >  Trying to match and modify part of a line in awk or sed
Trying to match and modify part of a line in awk or sed

Time:03-09

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

CodePudding user response:

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.

CodePudding user response:

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 {

CodePudding user response:

Using sed

$ sed -i.bak 's/_[0-9]\ /&_XX/' input_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 {
  • Related