2017-10-13 1 views

답변

1
awk ' {    # call awk 

     # file -> is array 
     # $0 -> current row/line/record 
     # here $0 is used array index/key 
     # ++ is post increment meaning 
     # if line was found before it will increment by 1 
     # so file[$0]++ holds count of occurrence of line 
     # suppose you got 4 lines, in that 3 are duplicates, then 
     # for such index file[<such_line>] will be 3 
     file[$0]++ 

     } 
     # end block as name suggests executes at the end 

    END { 
     # loop through array file 
     # j as index 
     for (j in file) { 

      # print array key, and array value 
      print j,file[j] 
     } 
     } 
    ' file 

예 :

$ cat infile 
lineA 
lineB 
lineA 
lineB 
lineA 
lineC 

$ awk ' {file[$0]++} END { for (j in file)print j,"repeated",file[j],"times in file :",FILENAME }' infile 
lineA repeated 3 times in file : infile 
lineB repeated 2 times in file : infile 
lineC repeated 1 times in file : infile 
+1

대단히 감사합니다. 너무 많이 도와 주셨습니다. – Mike

0

같은 당신을 도울 수에 따라.

awk ' { 
file[$0]++ ##creating an array named file and with respect to current line as index, incrementing its count each time with one if same entry comes in array file. 
} 
END {  ##Starts END section of awk code here. 
for (j in file) { ##starting for loop which will traverse through array file. 
print j,file[j] ##Printing value of j(which will be index of array named file(or other words current line value)) and printing the value of array file with index of j. 
} } ' 
+0

대단히 감사합니다, 당신은 저를 너무 많이 도왔습니다! – Mike

+1

@Mike, 도움이 되서 기쁩니다. https://stackoverflow.com/help/someone-answers를 참조하십시오. – RavinderSingh13

관련 문제