awk 스크립트를 실행할 때 표준 출력에 아무것도 없습니다.

awk 스크립트를 실행할 때 표준 출력에 아무것도 없습니다.

awk 스크립트를 실행합니다. 하지만 여기 있습니다. 이것은 명령입니다 awk -f awk_scr ERR.txt.

BEGIN {
FS=" " 
target="missing" 
}
{
for (i=1; i <= NR; i++) {
  for(j=1; j <= NF; j++) {
    if ($j == target) {
      do { 
        printf $j > "final.txt" 
      } while (j == NF)
    }           
    if (j == NF) {
      printf "\n"
    }
  }
}
}

내용은 ERR.txt여기에 있습니다. 이 awk 스크립트는 일치하는 콘텐츠를 다듬는 데 사용됩니다."잃어버린"그런 다음 파일로 인쇄하고,최종결과.txt.

npm ERR! peer dep missing: react@^15.0.0, required by [email protected]
npm ERR! peer dep missing: [email protected] - 3, required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]
npm ERR! missing: [email protected], required by [email protected]

그러나 명령을 실행하면 빈 표준 출력만 나타납니다.

================내용 추가=================

비록 몇 분 밖에 걸리지 않았지만 그것이 제가 예상했던 것입니다. 어쨌든 awk 스크립트는 앞으로도 계속 사용할 가치가 있습니다.

    missing: react@^15.0.0, required by [email protected]
    missing: [email protected] - 3, required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]
    missing: [email protected], required by [email protected]

답변1

# awk_scr

BEGIN {
    # clear the output file if it already exists
    # you can do this using your shell instead
    ORS = ""
    print "" > "final.txt"
    ORS = "\n"
}

/missing/ {
    # delete all characters prior to the last occurrence of "missing:"
    # this assumes that there is only one sub-string "missing:" per line
    sub(/.*missing:/, "missing:", $0)

    # append the updated line to the output file
    print $0 >> "final.txt"
}

그런 다음 awk -f awk_scr ERR.txt && cat final.txt.

sed또 다른 해결책은 더 짧은 것을 사용하는 것입니다 .

sed 's/^.*missing:/missing:/' ERR.txt | tee final.txt

그러나 awk 솔루션과 동일한 단점이 있습니다. 즉, "missing:"이 한 줄에 한 번만 발생한다고 가정합니다. 둘 다 탐욕스럽게 awk어울리기 때문이다 sed.

지연 일치를 시뮬레이션하려면 다음을 수행하면 됩니다(더 좋음).

sed -E 's/(missing)(.*)|./\1\2/g' ERR.txt | tee final.txt

답변2

나는 귀하의 스크립트가 약간 복잡하고 "awk"의 의미에 대한 오해를 보여주고 있다고 생각합니다. awk 프로그램은 다음 목록으로 구성됩니다.

pattern { actions }

성명. 새 레코드/행을 읽을 때마다 awk는 패턴 조작 문을 하나씩 실행하여 행을 처리합니다. 내가 보기에, 메모리에 있는 전체 파일을 awk로 읽는 것을 상상하고 있는 것 같습니다. 그런 다음 레코드 수와 함께 for 루프를 사용하여 이를 처리할 수 있습니다. 그런 일은 일어나지 않았습니다.

다음 awk가 귀하의 요청을 수행할 수 있습니다:

awk '/missing/' inputfile > "missing.txt"

여기서 모드는 이고 /missing/작업은 기본 작업 이며 전체 기록을 인쇄하거나 1동일합니다 . {print $0}이 모드 /missing/상태는 "missing" 하위 문자열을 포함하는 모든 행을 선택합니다.

관련 정보