ファイル内の文から重複する単語を削除する[重複]

Dec 08 2020

ファイル内の文から重複する単語を削除するにはどうすればよいですか?各文は別々の行に書かれています。

ありがとうございました

私はこれらの文章をファイルに持っています

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 ?

回答

1 dawg Dec 08 2020 at 01:28

できるよ:

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 ?