【问题标题】:removing duplicate words from sentences in file [duplicate]从文件中的句子中删除重复的单词[重复]
【发布时间】:2020-12-07 18:03:22
【问题描述】:

如何从文件中的句子中删除重复的单词,每个句子都写在单独的行中。

谢谢

我在文件中有这些句子

hello every body hello
word I should remove the word
how can can i remove it ?

预期的输出应该是

hello every body
word I should remove the
how can i remove it ?

【问题讨论】:

  • 考虑查看how do I ask a good question,然后回来更新您的问题;特别是,提供您的示例输入、您编写的代码、脚本生成的(错误)输出以及(正确)所需的输出
  • 请edit您的问题并显示您希望从示例输入中获得的预期输出。
  • @dwag 它是一样的,但它没有工作..
  • @dawg,它对他没有帮助 - 虽然它会去除欺骗,但它会剔除句子结构......

标签: linux bash shell unix


【解决方案1】:

你可以这样做:

awk '{for(i=1;i<=NF;i++) if(++arr[$i]==1) print $i}' file

打印:

hello
every
body
word
I
should
remove
the
how
can
i
it
?

保持线条结构:

awk '{for(i=1;i<=NF;i++) 
       if(++arr[$i]==1) 
          printf "%s%s", $i, OFS
       print ""}' file

打印:

hello every body 
word I should remove the 
how can i it ? 

如果重复数据删除仅基于每行:

awk '{delete arr
      for(i=1;i<=NF;i++) 
         if(++arr[$i]==1) printf "%s%s", $i, OFS
      print ""}' file

打印:

hello every body 
word I should remove the 
how can i remove it ? 

【讨论】:

  • 这样更好;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-03
  • 1970-01-01
相关资源
最近更新 更多