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

関連する問題