Search code examples
bashshellawkcut

Analyze a control table by Shell Script


A shell script is analysing a control table to get the right parameter for it's processing. Currently, it is simple - using grep, it points to the correct line, awk {print $n} determines the right columns.
Columns are separated by space only. No special rules, just values separated by space. All is fine and working, the users like it.
As long as none of the columns is left empty. For last colum, it's ok to leave it empty, but if somebody does not fill in a column in the mid, it confuses the awk {print $n} logic. Of course, one could as the users to fill in every entry, or one could just define the column delimiter as ";" . In case something is skipped, one could use " ;; " However, I would prefer not to change table style.
So the question is:
How to effectively analyze a table having blanks in colum values? Table is like this:

ApplikationService    ServerName    PortNumber     ControlValue_1    ControlValue_2    
Read                  chavez.com       3599         john                 doe    
Write                                  3345         johnny               walker    
Update                curiosity.org                 jerry                 

What might be of some help: If there is a value set in a column, it is (more a less precise) under its column header description.

Cheers,
Tarik


Solution

  • You don't say what your desired output is but this shows you the right approach:

    $ cat tst.awk
    NR==1 {
        print
        while ( match($0,/[^[:space:]]+[[:space:]]*/) ) {
            width[++i] = RLENGTH
            $0 = substr($0,RSTART+RLENGTH)
        }
        next
    }
    {
        i = 0
        while ( (fld = substr($0,1,width[++i])) != "" ) {
            gsub(/^ +| +$/,"",fld)
            printf "%-*s", width[i], (fld == "" ? "[empty]" : fld)
            $0 = substr($0,width[i]+1)
        }
        print ""
    }
    $
    $ awk -f tst.awk file
    ApplikationService    ServerName    PortNumber     ControlValue_1    ControlValue_2
    Read                  chavez.com    3599           john              doe
    Write                 [empty]       3345           johnny            walker
    Update                curiosity.org [empty]        jerry             [empty]
    

    It uses the width of the each field in the title line to determine the width of every field in every line of the file, and then just replaces empty fields with the string "[empty]" and left-aligns every field just to pretty it up a bit.