Sorry guys I had to edit my example, because I didn't express my query properly. Let's say I have the .txt file:
Happy sad
Happy sad
Happy sad
Sad happy
Happy sad
Happy sad
Mad sad
Mad happy
Mad happy
And I want to delete any string that is unique. Leaving the file with:
Happy sad
Happy sad
Happy sad
Happy sad
Happy sad
Mad happy
Mad happy
I understand that sort is able to get rid of duplicates (sort file.txt | uniq
), so is there anyway we can do the opposite in bash using a command? Or would I just need to figure out a while loop for it?
BTW uniq -D file.txt > output.txt
doesn't work.
printf '%s\n' a b b c a c
? Onlyb
, ora b b c a c
as they're all dupplicated even if not contiguous? Wouldb b a a c c
like in @Kusalananda's answer where output is delayed until the second occurrence be acceptable? – Stéphane Chazelas Nov 05 '20 at 13:34uniq -d
do what you need? – Thorbjørn Ravn Andersen Nov 07 '20 at 14:11