0

I have file and content has like below.

------------------------------------------------

##
servers:
# Start OF VM1
 - host: "VM1"
   queueFilters: 
       include: ["*"]
   channelFilters: 
       include: ["*"]
# End OF VM1

# Start OF VM2
- host: "VM2"
   queueFilters: 
       include: ["*"]
   channelFilters: 
       include: ["*"]
# End OF VM2
---------------------------------------------------

I wanted to update parameter like include under queueFilter section : ["test1","test2","test3"] only between the lines where from # Start OF VM1 to # End OF VM1

I tried with the command,

V1=VM1
V2="test1","test2","test3"

awk -F': ' '/# Start OF '$V1'/,/# End OF '$V1'/{if( $0 ~/include/ ) {if ( $2 ~ /\[\]/ ) {gsub(/\]/,"'$V2']")} else {gsub(/\]/,",'$V2']")}}}1' input.yaml

Could some one help me how to achieve this..

1 Answers1

0

First, you should assign the shell variables to awk variables, rather than trying to substitute shell variables into the script.

Second, if you only want to update queueFilters, you need to match that. You can set a variable that will be used when you get to the include line.

V1=VM1
V2='"test1","test2","test3"'

awk -F': ' -v V2="$V2" '
    /# Start OF '$V1'/,/# End OF '$V1'/ {
        if (/queueFilters:/) {inqueue = 1}
        else if ($1 ~ /include/ && inqueue) {
             if ( $2 ~ /\[\]/ ) {
                gsub(/\]/, V2 "]")
             } else {
                gsub(/\]/, "," V2 "]")
             }
             inqueue = 0
        }
    }1' input.yaml
Barmar
  • 741,623
  • 53
  • 500
  • 612