一步一步解释awk命令

时间:2017-10-13 08:58:31

标签: awk

你能解释一下我一步一步做这个命令吗?

awk ' {file[$0]++} END { for (j in file) {print j,file[j]} } '

2 个答案:

答案 0 :(得分:2)

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 :(得分:1)

以下可能对您有帮助。

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.
} } '