I want to take this file output:
Summary
-I- Stage Warnings Errors Comment
-I- Discovery 3 0
-I- Lids Check 0 0
-I- Links Check 0 0
-I- Subnet Manager 0 0
-I- Port Counters 0 0
-I- Nodes Information 16 0
-I- Speed / Width checks 0 15
-I- Virtualization 0 0
-I- Partition Keys 0 0
-I- Temperature Sensing 0 0
-I- Create IBNetDiscover File 0 0
and transform it using regex to this:
“Nodes Information section has 16 Warnings and 0 Errors”
“Speed / Width checks section has 0 Warnings and 15 Errors”
etc.... for each line.
I've tried using awk, but I can't manage to get the stage section without the word "Summary" and I don't know how to properly insert the words "section has" and "errors" in the right place.
I've only made it this far:
cat file.txt | awk -F' '"{2}" '/^-I-/{print $1}'
-I- Stage
-I- Discovery
-I- Lids Check
-I- Links Check
-I- Subnet Manager
-I- Port Counters
-I- Nodes Information
-I- Speed / Width checks
-I- Virtualization
-I- Partition Keys
-I- Temperature Sensing
-I- Create IBNetDiscover File
any suggestions?
Thanks
CodePudding user response:
This is one way to fix your formatting:
awk '/-I-/&&!/-I- Stage/ {
$0=substr($0,1,62);
s=substr($0,5,36);
gsub(/ *$/,"",s);
printf("\"%s section has %d Warnings and %d Errors\"\n",s,$(NF-1),$NF);
}' file.txt
/^-I-/&&!/^-I- Stage/
- Match on lines starting with-I-
but not-I- Stage
.$0=substr($0,1,62);
- Remove theComment
column from the input by only keeping the first 62 characters of the line.s=substr($0,5,36);
- Take the substring of the whole line starting at position 5 followed by 36 characters.gsub(/ *$/,"",s);
- In the strings
, replace the trailing spaces (/ *$/
) with""
(nothing)printf
\"
- A literal"
%s
- A string (which is taken from thes
variable we created above)%d
- An integer, where$(NF-1)
is the second last field and$NF
is the last field.
Output:
"Discovery section has 3 Warnings and 0 Errors"
"Lids Check section has 0 Warnings and 0 Errors"
"Links Check section has 0 Warnings and 0 Errors"
"Subnet Manager section has 0 Warnings and 0 Errors"
"Port Counters section has 0 Warnings and 0 Errors"
"Nodes Information section has 16 Warnings and 0 Errors"
"Speed / Width checks section has 0 Warnings and 15 Errors"
"Virtualization section has 0 Warnings and 0 Errors"
"Partition Keys section has 0 Warnings and 0 Errors"
"Temperature Sensing section has 0 Warnings and 0 Errors"
"Create IBNetDiscover File section has 0 Warnings and 0 Errors"
CodePudding user response:
Using sed
$ sed 's/ \ / /g;1,3d; s/.*- \([^0-9]\ \) \([^ ]*\) \(.*\)/"\1 section has \2 Warnings and \3 Errors"/' input_file
"Discovery section has 3 Warnings and 0 Errors"
"Lids Check section has 0 Warnings and 0 Errors"
"Links Check section has 0 Warnings and 0 Errors"
"Subnet Manager section has 0 Warnings and 0 Errors"
"Port Counters section has 0 Warnings and 0 Errors"
"Nodes Information section has 16 Warnings and 0 Errors"
"Speed / Width checks section has 0 Warnings and 15 Errors"
"Virtualization section has 0 Warnings and 0 Errors"
"Partition Keys section has 0 Warnings and 0 Errors"
"Temperature Sensing section has 0 Warnings and 0 Errors"
"Create IBNetDiscover File section has 0 Warnings and 0 Errors"