Issue
how can I remove the duplicate words from sentences in file ,each sentence written in a separate line.
thank you
I have these sentences in file
hello every body hello
word I should remove the word
how can can i remove it ?
the expected out put should be
hello every body
word I should remove the
how can i remove it ?
Solution
You can do:
awk '{for(i=1;i<=NF;i++) if(++arr[$i]==1) print $i}' file
Prints:
hello
every
body
word
I
should
remove
the
how
can
i
it
?
To maintain the line structure:
awk '{for(i=1;i<=NF;i++)
if(++arr[$i]==1)
printf "%s%s", $i, OFS
print ""}' file
Prints:
hello every body
word I should remove the
how can i it ?
If the deduplication is only on a per line basis:
awk '{delete arr
for(i=1;i<=NF;i++)
if(++arr[$i]==1) printf "%s%s", $i, OFS
print ""}' file
Prints:
hello every body
word I should remove the
how can i remove it ?
Answered By - dawg