Search code examples
linuxbashunixawkxargs

How To Remove Duplicate Words In Single Line When Piping Multiple Lines


Right now, I am piping some lines through the command xargs -n1 | sort -u | xargs (which I found here: Sorting and removing duplicate words in a line).

But, my problem is that when piping multiple lines, it puts all the lines together. How can I pipe through:

My first example line for example
My second example line for example
My third example line for example

And get:

My first example line for
My second example line for
My third example line for

Since I'm running a lot of pipes before this, and it will be running within a loop, I would prefer if you know any simple solutions without creating a new loop and reading line by line. I just want it to work for one line at a time so I can just put it right into the loop. Thanks for the consideration.


Solution

  • $ 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