1832602 Members
2635 Online
110043 Solutions
New Discussion

Re: Format

 
SAM_24
Frequent Advisor

Format

Hi,

How to delete unique lines based on particular field?

word1 TIME word2 tdce-512 word4
word1 TIME word2 tdce-512 word4
word1 TIME word2 tdce-512 word4
word1 TIME word2 tdce-513 word4
word1 TIME word2 tdce-517 word4

I want to delete duplicate lines based on fourth field.

Any help is appreciated.

Thanks.
Never quit
5 REPLIES 5
Jeff_Traigle
Honored Contributor

Re: Format

Assuming the input is in a file named filename and you meant you want to keep one unique entry based on the fourth field, this should do it (-u for unique, -k4 for fourth field as key):

sort -u -k4 filename
--
Jeff Traigle
Sundar_7
Honored Contributor

Re: Format

Try this

sort -buk 4 < input-file > output-file
Learn What to do ,How to do and more importantly When to do ?
Hein van den Heuvel
Honored Contributor

Re: Format

sort will do the job, bot of course it will do the sort also.

If you need to keep the original order, yet must only retain the first of a duplicate then you can use a perl (or awk) script like:

perl -ne '$x=(split)[3];next if $seen{$x};$seen{$x}++;print' file.
Hein van den Heuvel
Honored Contributor

Re: Format


Hmmm, you assigned 4 points to each solutions presented, suggesting that you did evaluate them, but that they did NOT solve your problem.
Maybe you can clarify what is missing based on the results from the earlier replies? Maybe a more clear 'before' and 'after' picture, perhaps in a text attachment, is needed?

Regards,
Hein.
Rodney Hills
Honored Contributor

Re: Format

If you are looking to not use sort, then how about awk-

awk '{if (x != $4) { print $0}; x=$4}'

HTH

-- Rod Hills
There be dragons...