如何在管道多行时删除单行中的重复单词
How To Remove Duplicate Words In Single Line When Piping Multiple Lines
现在,我正在通过命令 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
因为我在这之前 运行 有很多管道,而且它会在一个循环中 运行,如果你知道任何简单的解决方案而无需创建新循环并阅读逐行。我只希望它一次只处理一行,这样我就可以将它直接放入循环中。感谢您的考虑。
$ 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
稍微简单一点的 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
现在,我正在通过命令 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
因为我在这之前 运行 有很多管道,而且它会在一个循环中 运行,如果你知道任何简单的解决方案而无需创建新循环并阅读逐行。我只希望它一次只处理一行,这样我就可以将它直接放入循环中。感谢您的考虑。
$ 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
稍微简单一点的 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