[SOLVED] Is there a program that removes non-adjacent duplicate lines?
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Sounds like you are looking for a "uniq" command that does not require source file to be sorted and is not interactive.
I do not think there is such a command.
However; in the past I dabbled with using this Windows based program running under Wine: WinMerge
You could do it with AWK and an associative array keyed on the contents of each line. I'm not sure how well that would scale though. How large a text file are you considering?
While I will not now write the "necessary one-liner" for you, the algorithm essentially is this:
• Produce a version of the input file which contains a "record number" field to the left of the record's contents.
• Sort the resulting file by the second field: the actual content
• Now that the "duplicate values" are adjacent, considering only the second field remove all but the first occurrence. This is trivial now, because you need only consider the current value against its immediate predecessor.
• Re-sort the resulting file by the first ("record number") field.
• Remove the "record number" field to produce the final result.
Fifty years ago, they did this with magnetic tapes. It may well be that they did it earlier using punched cards.
Last edited by sundialsvcs; 11-15-2022 at 11:48 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.