2016-12-06 76 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