Reputation: 123
I have a requirement as mentioned below.
I have to access the last column from the table, shown below, of data (obtained from the remote node), using an Ansible playbook.
PROGRESS: Starting connection towards ss7caf stack...
Please wait, the output can take up to several minutes
Blade | EP | AS | Paths (* - current data | Path
Id | Id | Id | transfer path) | State
------+----+-----+-------------------------------+---------
PL-3 | 1 | 1-1 | 10.201.176.0 - 10.116.178.161 | Active
| | | 10.201.176.0 - 10.116.178.162 | Blocked
| | | 10.201.176.2 - 10.116.178.161 | Blocked
| | | 10.201.176.2 - 10.116.178.162 | Inactive
PL-4 | 1 | 4-1 | 10.201.176.0 - 10.250.161.130 | Active
| | | 10.201.176.0 - 10.250.161.138 | Blocked
| | | 10.201.176.2 - 10.250.161.130 | Inactive
This table was generated: 2022-03-08 11:11:38 AEDT
I have to use the content of the last column in a conditional statement to draw some conclusions based on it.
For example, the paths which are specified in the last but one column, corrsponding to the blade ID mentioned in column 1
I have tried a playbook like this.
---
# The objective of this playbook is to use this command "sudo dsc-show-sctp-assoc"
# and check the status of Check the status of SCTP Associations.
- name: Playbook for Checking the status of SCTP Associations
hosts: all
become: true
tasks:
- name: Getting SCTP Association status details and saving them to a text file
shell: dsc-show-sctp-assoc | tee sctp.txt # "dsc-show-sctp-assoc" is used to get the tabular data from the remote node
- name: IDNETIYING THE PASS AND FAIL CASES
shell: cat sctp.txt | awk -F "|" {if ($NF=="Inactive") print $0, " ", '===>', "FAIL"; else print $0, " ", "====>","PASS";}'
register: pass_fail_cases
- name: PRINTING THE PASS AND FAIL CASES
debug:
msg: "{{pass_fail_cases.stdout_lines }}"
...
The expected output is shown below
PROGRESS: Starting connection towards ss7caf stack...
Please wait, the output can take up to several minutes
Blade | EP | AS | Paths (* - current data | Path
Id | Id | Id | transfer path) | State
------+----+-----+-------------------------------+---------
PL-3 | 1 | 1-1 | 10.201.176.0 - 10.116.178.161 | Active ===> "PASS"
| | | 10.201.176.0 - 10.116.178.162 | Blocked ===> "PASS"
| | | 10.201.176.2 - 10.116.178.161 | Blocked ===> "PASS"
| | |*10.201.176.2 - 10.116.178.162 | Inactive ===> "FAIL"
PL-3 | 1 | 4-1 | 10.201.176.0 - 10.250.161.130 | Active ===> "PASS"
| | | 10.201.176.0 - 10.250.161.138 | Blocked ===> "PASS"
| | | 10.201.176.2 - 10.250.161.130 | Inactive ===> "FAIL"
This table was generated: 2022-03-08 11:11:38 AEDT
whereas I am getting the below output
PROGRESS: Starting connection towards ss7caf stack...
Please wait, the output can take up to several minutes
Blade | EP | AS | Paths (* - current data | Path
Id | Id | Id | transfer path) | State
------+----+-----+-------------------------------+---------
PL-3 | 1 | 1-1 | 10.201.176.0 - 10.116.178.161 | Active ===> "PASS"
| | | 10.201.176.0 - 10.116.178.162 | Blocked ===> "PASS"
| | | 10.201.176.2 - 10.116.178.161 | Blocked ===> "PASS"
| | |*10.201.176.2 - 10.116.178.162 | Inactive ===> "PASS"
PL-3 | 1 | 4-1 | 10.201.176.0 - 10.250.161.130 | Active ===> "PASS"
| | | 10.201.176.0 - 10.250.161.138 | Blocked ===> "PASS"
| | | 10.201.176.2 - 10.250.161.130 | Inactive ===> "PASS"
This table was generated: 2022-03-08 11:11:38 AEDT
I guess that the conditional statement (if condition) of AWK command is not working as expected and hence everything is resulted as "PASS".
I tried it multiple ways. But none of them gave me the expected output.
Can someone please help to let me know where I am going wrong?
PS: I tried using AWK command here. Please suggest me the other approcahes that I can follow to get the expected output, if any
Upvotes: 3
Views: 226
Reputation: 2865
{m,g}awk '!/[|+]/ || _+__*(_ -= /^[+-]+$/) || sub("$", " ===> \"" substr(\
"PASSFAIL", 5^(!_<NF), 4*!+_) "\"")' FS='[|] Inactive[ \t]*$' \_=1
######### # or preferably, not hard-coding in the "1" :
mawk 'BEGIN { FS = "[|][ \t]+Inactive[ \t]*$"
_+=_+=_=__ ^= _<_
} !/[|+]/ || __ + (__ -= /^[+-]+$/)*-!_ || sub("$",\
" ===> \"" substr("PASSFAIL", (--_+--_)^(_==NF), _^=_)'
=
PROGRESS: Starting connection towards ss7caf stack...
Please wait, the output can take up to several minutes
Blade | EP | AS | Paths (* - current data | Path
Id | Id | Id | transfer path) | State
------+----+-----+-------------------------------+---------
PL-3 | 1 | 1-1 | 10.201.176.0 - 10.116.178.161 | Active ===> "PASS"
| | | 10.201.176.0 - 10.116.178.162 | Blocked ===> "PASS"
| | | 10.201.176.2 - 10.116.178.161 | Blocked ===> "PASS"
| | | 10.201.176.2 - 10.116.178.162 | Inactive ===> "FAIL"
PL-4 | 1 | 4-1 | 10.201.176.0 - 10.250.161.130 | Active ===> "PASS"
| | | 10.201.176.0 - 10.250.161.138 | Blocked ===> "PASS"
| | | 10.201.176.2 - 10.250.161.130 | Inactive ===> "FAIL"
This table was generated: 2022-03-08 11:11:38 AEDT
Upvotes: 0
Reputation: 203995
$ awk '
BEGIN { map["Active"]=map["Blocked"]="PASS"; map["Inactive"]="FAIL" }
{ print $0 ($NF in map ? " ===> \"" map[$NF] "\"" : "") }
' sctp.txt
PROGRESS: Starting connection towards ss7caf stack...
Please wait, the output can take up to several minutes
Blade | EP | AS | Paths (* - current data | Path
Id | Id | Id | transfer path) | State
------+----+-----+-------------------------------+---------
PL-3 | 1 | 1-1 | 10.201.176.0 - 10.116.178.161 | Active ===> "PASS"
| | | 10.201.176.0 - 10.116.178.162 | Blocked ===> "PASS"
| | | 10.201.176.2 - 10.116.178.161 | Blocked ===> "PASS"
| | | 10.201.176.2 - 10.116.178.162 | Inactive ===> "FAIL"
PL-4 | 1 | 4-1 | 10.201.176.0 - 10.250.161.130 | Active ===> "PASS"
| | | 10.201.176.0 - 10.250.161.138 | Blocked ===> "PASS"
| | | 10.201.176.2 - 10.250.161.130 | Inactive ===> "FAIL"
This table was generated: 2022-03-08 11:11:38 AEDT
By the way, in your code you have:
shell: cat sctp.txt | awk -F "|" {if ($NF=="Inactive") print $0, " ", '===>', "FAIL"; else print $0, " ", "====>","PASS";}'
idk why you're swapping back and forth between singe and double quotes like that but - don't. A valid syntax for what that line is trying to do would be:
shell: cat sctp.txt | awk -F "|" '{if ($NF=="Inactive") print $0, " ", "===>", "FAIL"; else print $0, " ", "====>","PASS";}'
which could be improved to (but still won't work, I'm just showing valid syntax, not correcting semantics):
shell: awk -F '|' '{print $0, " ", "===>", ($NF=="Inactive" ? "FAIL" : "PASS")}' sctp.txt
Upvotes: 0
Reputation: 133610
With your shown samples only, please try following awk
code.
awk '
!NF{ print; found="" }
(/^(PROGRESS|[[:space:]]+|Blade|Id|-{6}\+)|^This table was generated:/ && !found){
if(/^-{6}\+/){ found=1 }
print
next
}
found{
print $0, "===>", ($NF ~ /Inactive/ ? "\"FAIL\"":"\"PASS\"")
}
' Input_file
OR in case you want to use it as a one-liner to use it in ansible playbook try following one-liner then:
awk '!NF{print;found=""} (/^(PROGRESS|[[:space:]]+|Blade|Id|-{6}\+)|^This table was generated:/ && !found){if(/^-{6}\+/){found=1};print;next} found{print $0, "===>", ($NF ~ /Inactive/ ? "\"FAIL\"":"\"PASS\"")}' Input_file
Explanation: Adding detailed explanation for above.
awk ' ##Starting awk program from here.
!NF{ print; found="" } ##If NF is NULL then print that line and Nullify found here.
(/^(PROGRESS|[[:space:]]+|Blade|Id|-{6}\+)|^This table was generated:/ && !found){ ##Checking conditions if line starts from PROGRESS OR spaces OR Blade OR Id OR 6 dashes then do following.
if(/^-{6}\+/){ found=1 } ##Checking if line starts from 6 dashes then set found to 1.
print ##Printing line here.
next ##next will skip all further statements from here.
}
found{ ##Checking condition if found is NOT NULL.
print $0, "===>", ($NF ~ /Inactive/ ? "\"FAIL\"":"\"PASS\"") ##Then printing current line followed by either Fail or Pass based on last field as per OP request.
}
' Input_file ##Mentioning Input_file name here.
Upvotes: 2
Reputation: 785551
Following awk
should work for you:
awk -F '|' '!NF {main=0} !main; /^-{3}/ {main=1; next} main {print $0, "===>", ($NF ~ /Inactive/ ? "\"FAIL\"" : "\"PASS\"")}' sctp.txt
PROGRESS: Starting connection towards ss7caf stack...
Please wait, the output can take up to several minutes
Blade | EP | AS | Paths (* - current data | Path
Id | Id | Id | transfer path) | State
------+----+-----+-------------------------------+---------
PL-3 | 1 | 1-1 | 10.201.176.0 - 10.116.178.161 | Active ===> "PASS"
| | | 10.201.176.0 - 10.116.178.162 | Blocked ===> "PASS"
| | | 10.201.176.2 - 10.116.178.161 | Blocked ===> "PASS"
| | | 10.201.176.2 - 10.116.178.162 | Inactive ===> "FAIL"
PL-4 | 1 | 4-1 | 10.201.176.0 - 10.250.161.130 | Active ===> "PASS"
| | | 10.201.176.0 - 10.250.161.138 | Blocked ===> "PASS"
| | | 10.201.176.2 - 10.250.161.130 | Inactive ===> "FAIL"
This table was generated: 2022-03-08 11:11:38 AEDT
Or to make it readable:
awk -F '|' '
!NF {main=0}
!main
/^-{3}/ {main=1; next}
main {
print $0, "===>", ($NF ~ /Inactive/ ? "\"FAIL\"" : "\"PASS\"")
}' sctp.txt
This solutions uses a flag called main
to indicate when your main body of report starts. Initially main
is zero, when we encounter -----
line then we set main=1
. Last block adds PASS
or FAIL
when main==1
depending on value of $NF
.
Upvotes: 3