2016-12-06 66 views
-1

相同的領域,我想提出一些類型的數據:AWK不重複的記錄

A;01;data_1;CP 
A;01;data_15;aP 
A;01;data_23;Com 
A;01;data_106;id 

這樣

A;01;data_1;CP 
;;data_15;aP 
;;data_23;Com 
;;data_106;id 

有沒有一種簡單的方法使用awk做到這一點?

任何幫助表示讚賞!

+1

你應該嘗試添加有關問題的更多信息。例如。如何從輸入中獲得輸出。 etc – nu11p01n73R

+0

此外,它預計僅列1,2或可能在任何地方? – Inian

回答

1

是有,不知道這是一個簡單的...

awk 'BEGIN{FS=OFS=";"}{for(i=1;i<=NF;i++) if($i==a[i]) $i="";else a[i]=$i }1' file 

腳本通過各條線和空白的所有參數設置的輸入和輸出的分隔符​​;

循環參數內容如果這個參數與最後一行相同。

+0

這實際上與我的答案相同,但在我之前幾秒鐘發佈。看起來我們有完全相同的想法。 :) – hek2mgl

+0

@ hek2mgl在同一時間確實是同一個想法:-)唯一的一個小區別是''printt'語句在這裏被'1'替代,這會觸發'awk'中的默認操作,也就是打印整行。 – oliv

1

您可以使用下面的腳本awk

# dedup.awk 

BEGIN { 
    # Setting input and output delimiter to ';' 
    FS=OFS=";" 
} 

{ 
    # Iterate trough all fields 
    for(i=1;i<NF+1;i++) { 
     # If the previous record's field at this index has 
     # the same value as this field then set this field 
     # to an empty string 
     if(p[i]==$i) { 
      $i="" 
     } else { 
      # Otherwise update the array that keeps 
      # information about the previous record(s) 
      p[i] = $i 
     } 
    } 
    # Print the record 
    print 
} 

您可以執行這樣的:

awk -f dedup.awk input.file