Skip to main content
it's all awk
Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 264
Source Link
dimas
  • 1.2k
  • 4
  • 23
  • 33

AWK script function body clarification

I was reviewing one bash script and am stuck despite my googling efforts to decipher as to how this particular function works.

I got the part for gettimestamp & printeverything works and how it is called. However starting with ORA up until Housetype I am not sure how it works. According to this AWK tutorial there are predefined functions within AWK and it always starts with function but I couldn't see any reference or examples for the code that is enclosed in "<---Section is confusing---->". I am not asking for anyone to explain the whole code but in particular if you can explain at least how /^DocType|/ works section. Please see my comments below.

    func_sql()
    {
            ITMF=$TMF.2
            _retrieve | _run_sqlplus 2>&1 | ${_APP_AWK} -vtmf=$ITMF '
    
            BEGIN {
                    FS = "^B";
                    cnt=0;
                    printf("umask 007;\n") >>tmf
                    printf("cd %s;\n", imgDir) >>tmf
            }
            
            function getTimeStamp(s) {
                    printf("%s %s\n", strftime("[%a %b %d %H:%M:%S]"), s) >>logFile
            }
    
            function printEverything(s) {
                    printf("<P>%s %s\n", strftime("[%a %b %d %H:%M:%S]"), s);
                    printf("%s %s\n",
                            strftime("[%a %b %d %H:%M:%S]"),
                            s) >>logFile
            }
    <--- This section is confusing ------>
            /^ORA-.*:|^PLS-.*:|^SP2-.*/ { <-- I don't understand this part
                    getTimeStamp($0) <--- I understand this
                    printf("\nSQLBAD|1000|%s\n", $0); <--- I understand this
                    exit(1); <--- I understand this
            }
    
            /^ERROR/ { <-- I don't understand this part
<--Truncated-->
            }
    
            /\[.*\]\|Error.*/ { <-- I don't understand this part
                    <--Truncated-->
            }
    
            /^HouseType\|/ { <-- I don't understand this part
                    gsub("[[:space:]]+", " ");<--- I understand this
                    gsub("^[[:space:]]+", "");<--- I understand this
                    gsub("[[:space:]]+$", "");<--- I understand this
                    gsub("[[:space:]]+^B", "^B");<--- I dont' know this bit, what does ^B stands for?
                    gsub("^B[[:space:]]+", "^B");<--- I dont' know this bit, what does ^B stands for?
    
                    if($0 == "")
                            next;
    
                    print "<option value=\"" $2 "\">" $3 "</option>";
    
                    next;
            }
            {
                    gsub("[[:space:]]+", " ");
                    gsub("^[[:space:]]+", "");
                    gsub("[[:space:]]+$", "");
    
                    if($0 == "")
                            next;
            }
    <--- This section is confusing ------>    
            END {
                    printf("cnt=%s\n", cnt);
            }
            '