2017-10-13 90 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