AWK 스크립트 함수 본문 설명

AWK 스크립트 함수 본문 설명

나는 bash 스크립트를 검토하고 있으며 이 특정 기능이 어떻게 작동하는지 해독하기 위해 인터넷 검색을 통해 노력했지만 막혔습니다.

나는 gettimestamp를 얻었고 모든 부분과 그것이 호출되는 방식을 인쇄했습니다. 그러나 ORA부터 Housetype까지 어떻게 작동하는지 잘 모르겠습니다. 이 AWK에 따르면지도 시간AWK에는 미리 정의된 함수들이 있고 항상 function으로 시작하는데 "<---Section is Albany---->"에 포함된 코드에 대한 참조나 예시를 볼 수 없습니다. 누구에게 전체 코드를 설명해달라고 요청하는 것은 아니지만, 특히 /^DocType\|/ 부분이 어떻게 작동하는지 최소한 설명할 수 있다면 더욱 그렇습니다. 아래 내 의견을 참조하세요.

    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);
            }
            '

답변1

awk당신이 이해하지 못하는 부분이 많은 기능을 가져오는 정규식 트리거 구문이라고 말하는 것 같습니다 . 단순화하기 위해 스크립트의 스캐폴딩을 awk다음과 같이 설명할 수 있습니다.

BEGIN {
    Things to do before processing data
}

/needle/  {
    Things to do when a record contains a match for the regex /needle/
}

expression {
    Things to do for each record when the expression evaluates to true (i. e. nonzero)
}

{
    Things to do for all input records
}

END {
    Things to do after all records have been processed
}

인용한 줄을 확장하려면:

/^ORA-.*:|^PLS-.*:|^SP2-.*/ {
    stuff
}

/^ORA-.*:|^PLS-.*:|^SP2-.*/다음 조건 중 하나와 일치하는 문자열과 일치하는 정규식입니다.

  • 로 시작 ORA-하고 그 뒤에 :0개 이상의 후속 문자가 옵니다.
  • 로 시작 PLA-하고 그 뒤에 :0개 이상의 후속 문자가 옵니다.
  • 에 의해. . 시작SP2-

표현식 뒤의 중괄호 안의 코드는 일치하는 레코드에서 실행됩니다.

/^ERROR/ {
    stuff
}

로 시작하는 모든 문자열과 일치하는 간단한 정규식입니다 ERROR.

/\[.*\]\|Error.*/ {
    stuff
}

또 다른 정규식은 이번에는 일치하는 대괄호 쌍으로 시작하는 문자열 또는 string 으로 시작하는 모든 항목과 일치합니다 Error.

gsub("[[:space:]]+^B", "^B");
gsub("^B[[:space:]]+", "^B");

이는 첫 번째 경우에 Ctrl+ 문자가 뒤따르는 공백과 일치하는 일련의 문자를 대체하거나, B두 번째 경우에는 단순한 +로 Ctrl역순으로 대체합니다 B. 이 제어 문자는 BEGIN섹션에서 필드 구분 기호로 정의되어 있음을 기억하십시오.

관련 정보