【问题标题】:How To Remove Duplicate Words In Single Line When Piping Multiple Lines管道多行时如何删除单行中的重复单词
【发布时间】:2018-03-22 09:14:09
【问题描述】:

现在,我正在通过命令 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

由于在此之前我运行了很多管道,并且它将在一个循环中运行,如果您知道任何简单的解决方案而不创建新的循环并逐行读取,我会更喜欢。我只希望它一次只为一行工作,所以我可以把它直接放入循环中。感谢您的考虑。

【问题讨论】:

    标签: linux bash unix awk xargs


    【解决方案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
    

    【讨论】:

      【解决方案2】:

      更简单的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 
      

      【讨论】:

        猜你喜欢
        • 2018-11-20
        • 1970-01-01
        • 2018-07-27
        • 1970-01-01
        • 2021-10-03
        • 2012-02-06
        • 1970-01-01
        • 1970-01-01
        • 2020-04-02
        相关资源
        最近更新 更多