Friday, May 27, 2022

[SOLVED] How to parse a table with spaces using regex and add text?

Issue

I want to take this file output:

Summary

-I- Stage                               Warnings   Errors     Comment

-I- jamaico                                3          0

-I- jamaico1 Check                         0          0

-I- jamaico Check                          0          0

-I- jamaico Manager                        0          0

-I- jamaico Counters                       0          0

-I- jamaico Information                    16         0

-I- jamaico / Width checks                 0          15

-I- jamaico                                0          0

-I- jamaico Keys                           0          0

-I- jamaico Sensing                        0          0

-I- Create jamaico File                    0          0

and transform it using regex to this:

“jamaico Information section has 16 Warnings and 0 Errors”

“jamaico / 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- jamaico
-I- jamaico1 Check
-I- jamaico Check
-I- jamaico Manager
-I- jamaico Counters
-I- jamaico Information
-I- jamaico / Width checks
-I- jamaico
-I- jamaico Keys
-I- jamaico Sensing
-I- Create linux File

any suggestions?

Thanks


Solution

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 the Comment 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 string s, replace the trailing spaces (/ *$/) with "" (nothing)
  • printf
    • \" - A literal "
    • %s - A string (which is taken from the s 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"


Answered By - Ted Lyngmo
Answer Checked By - Robin (WPSolving Admin)