Skip to content
Advertisement

Bash script how to display matched words in custom order

The following is the output of the command ads2 cls create

kernel with pid 7148 (port 9011) killed
kernel with pid 9360 (port 9011) killed
probing service daemon @ http://fdt-c-vm-0093.fdtech.intern:9010
starting kernel FDT-C-VM-0093 @ http://fdt-c-yy-0093.ssbt.intern:9011 name=FDT-C-VM-0093 max_consec_timeouts=10 clustermode=Standard hostname=FDT-C-VM-0093 framerate=20000 schedmode=Standard rtaddr=fdt-c-vm-0093.fdtech.ssbt tickrole=Local tickmaster=local max_total_timeouts=1000
kernel FDT-C-VM-0093 running
probing service daemon @ http://172.16.xx.xx:9010
starting kernel FDT-C-AGX-0004 @ http://172.16.xx.xx:9011 name=FDT-C-AGX-0004 max_consec_timeouts=10 clustermode=Standard hostname=FDT-C-AGX-0004 framerate=20000 schedmode=Standard rtaddr=172.16.xx.xx tickrole=Local tickmaster=local max_total_timeouts=1000
kernel Fxx-x-xxx-xxx4 running
>>> start cluster establish ...
>>> cluster established ...
        nodes {
            node {
                name = "FDT-C-VM-xxxx";
                address = "http://fxx-x-xx-0093.xxx.intern:xxxx/";
                state = "3";
            }
            node {
                name = "xxx-x-xxx-xxx";
                address = "http://1xx.16.xx.xx:9011/";
                state = "3";
            }
        }

However, I’m trying to extract the value of name and state for each node, save them in a variable and display them in the following order:

**#example output**

Node fdt-c-agx-xxx has state 3
Node FDT-C-VM-xxx has state 3

Till now i, with the help of this so much powerful learning site, could extract the values of name and state by executing the following:

cls="$(ads2 cls create | grep '(state|name) =' | cut -d '"' -f 2)" 

Now if i print cls variable, i get the following:

FDT-C-VM-xxx
3
FDT-C-AGX-xxx
3

First Question:

How can i display the result like the one above in the **#example output**?

Second question

With this implementation, how can I check the value of state varaiable for both nodes in order to print something like

if node1State = 3 && node2State = 3; then
echo "Sucess"
else 
echo "Failed"

Advertisement

Answer

Since the output seems to be json, you should really use a json parser such as jq but in the absence of jq you can use awk and combine the requirements for question one and two:

ads2 cls create | awk -F ["] '/^>>> cluster established .../ { strt=1 } strt!=1 { next } $1 ~ "name" { cnt++;nam[cnt]=$2 } $1 ~ "state" { stat[cnt]=$2;print "Node "nam[cnt]" has state "$2 } END { if (stat[1]=="3" && stat[2]=="3") { print "Success" } else { print "Failed" } }'  

Explanation:

 ads2 cls create | awk -F ["] '                                         # Set the field delimiter to a double quote
/^>>> cluster established .../ { 
               strt=1                                                    # If the line starts with ">>> cluster established ...", set a variable strt to 1
            }
 strt!=1    { 
               next                                                      # If strt is not equal to 1, skip to the next line
            }
 $1 ~ "name" { 
               cnt++;                                                    # If the first field contains name, increment a cnt variable
               nam[cnt]=$2                                               # Use the cnt variable as the index of an array called nam with the second field the value
             }  
 $1 ~ "state" {   
               stat[cnt]=$2;                                             # When the first field contains "state", set up another array called stat
               print "Node "nam[cnt]" has state "$2                      # Print the node name as well as the state
               } 
           END { 
                 if (stat[1]=="3" && stat[2]=="3") { 
                   print "Success"                                       # At the end of processing, use the array to determine whether there is a success of failure.
                 } 
                 else { 
                   print "Failed" 
                 } 
                }'
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement