2017-10-13 1 views

답변

1
awk 'FNR > 1 && dateA <= $5 ' FS='|' dateA="$dateA" "$infile" 
  • FNR는 한 awk 먼저 파일을 읽고으로 동일합니다 변수 NR, FNRNR 값을 혼동하지 마십시오 변수가 당신에게 현재 파일에 관련 기록의 총 수를 제공입니다 , 두 번째 파일의 경우 FNR 변수는 재설정되지만 NR 변수는 재설정되지 않습니다.

이것은 얼마나 기록을 읽을없이이 1보다 큰 변수 dateA이 5 번째 필드/열보다 작거나 같은 경우 awk

$ seq 1 5 >file1 
$ seq 1 3 >file2 
$ cat file1 
1 
2 
3 
4 
5 

$ cat file2 
1 
2 
3 

$ awk '{print "Current line : "$0,"File: "FILENAME,"FNR : ",FNR,"NR : ",NR}' file1 file2 
Current line : 1 File: file1 FNR : 1 NR : 1 
Current line : 2 File: file1 FNR : 2 NR : 2 
Current line : 3 File: file1 FNR : 3 NR : 3 
Current line : 4 File: file1 FNR : 4 NR : 4 
Current line : 5 File: file1 FNR : 5 NR : 5 
Current line : 1 File: file2 FNR : 1 NR : 6 
Current line : 2 File: file2 FNR : 2 NR : 7 
Current line : 3 File: file2 FNR : 3 NR : 8 
  • FNR > 1 && dateA <= $5에서 FNRNR 작품, 우리 boolean true 상태가되므로 해당 라인이 인쇄됩니다.

  • FS='|'(3210)는 입력 필드 분리는 또한

    • awk -F'|' '{ .... }' OR
    • awk -v FS='|' '{ .... }' OR
    • awk 'BEGIN{FS="|"}{ .... }'
  • dateA="$dateA"dateA

  • 같이 설정할 수있다 치가 쉘 변수 $dateA에서 촬영 awk 변수 , 비슷하게 설정할 수 있습니다.

    ,210
    • awk -v dateA="$dateA" '{ .... }'
    • 귀하의 위의 명령 아래도

      awk -F'|' -v dateA="$dateA" 'FNR>1 && dateA <= $5' "$infile" 
      

      처럼 다시 작성하고 당신은 또한

      awk -F'|' -v dateA="$dateA" 'FNR>1 && dateA <= $5{ print }' "$infile" 
                  ^    ^
                  |     | 
                If this condition is true | 
                       | 
                  Action is to print line, 
                  print or print $0 is same 
      
      로 쓸 수 있도록 어떤 사람들은 더 나은 읽기 awk 'condition{action}'를 선호 할 수

0

AWK 폼 var=value으로 인수 내부 변수를 할당 할 수 있습니다. AWK는 셸 변수에 액세스 할 수 없으므로 dateA="$dateA"dateA을 AWK 스크립트로 "내보내는"데 사용됩니다.

$ echo >file1; echo >file2 
$ awk -vx=0 ' 
     BEGIN { 
       print "BEGIN", x 
     } 
     { 
       print FILENAME, x 
     } 
     END { 
       print "END", x 
     }' x=1 file1 x=2 file2 x=3 
BEGIN 0 
file1 1 
file2 2 
END 3 
1

다음과 같은 설명을 통해 이동이 당신을 도움이 있으면 알려 주시기 바랍니다 : 할당 인수 BEGIN 후, 파일 처리 중에 발생 및 파일 사이에-사용할 수 있다는

참고.

설명 : 다음 awk에서 실행하지 마십시오. 설명 용으로 만 확장되었습니다.

awk ' 
FNR>1 && dateA<=$5 ##FNR denotes the number of current line in awk so here 2 conditions with AND conditions are being checked. 
        ##1st is if current line number is greater than 1 and second is variable named dateA value should be lesser 
        ##and equal to 5. 
        ##So let me explain here awk works on method of condition and then action, so if any condition is TRUE then action 
        ##will happen, here condition is there but NO action defined, so by default print action will happen. print of 
        ##current line. 
' 
FS='|'    ##FS denotes the field separator, in awk we could define the field separator by ourselves too, so making it here as | 
dateA="$dateA"  ##creating variable named dateA whose value is equal to shell variable named dateA. In awk if we have to assign 
        ##shell variable values to awk variables we have to create an awk variable and then assign shell variable value to 
        ##it. 
"$infile"   ##Mentioning the Input_file name here which awk has to go through. Point to be noted here the "$infile" means 
        ##it is a shell variable (as we all know to print shell variable value we have to use "$infile")