2017-10-10 77 views
2

现在,我通过命令xargs -n1 | sort -u | xargs(我在这里找到:Sorting and removing duplicate words in a line)管道一些行。如何删除单行中的重复单词当管道多行

但是,我的问题是,当管道多条线路,它把所有的线路放在一起。我怎样才能管通过:

My first example line for example 
My second example line for example 
My third example line for example 

并获得:

My first example line for 
My second example line for 
My third example line for 

由于我跑了很多管道在此之前,它会在一个循环中运行,如果你知道我宁愿任何简单的解决方案,无需创建新的循环和逐行读取。我只是希望它一次只能用于一行,因此我可以将它放在循环中。感谢您的考虑。

回答

1
$ awk '{delete seen; c=0; for (i=1;i<=NF;i++) if (!seen[$i]++) printf "%s%s", (++c>1?OFS:""), $i; print ""}' file 
My first example line for 
My second example line for 
My third example line for 
0

一点点简单的awk程序,但不保留顺序(既不您的管道呢,所以我假设这是不是你的要求):

$ awk '{delete a; for(i=1;i<=NF;i++) a[$i]; for(w in a) printf w" ";print ""}' lines 
first line My for example 
line My for second example 
line My third for example