ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
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.
Trying to write a script that will delete items that are NOT in a file. For example:
Quote:
list.txt:
twantrd
joe
mary
list1.txt:
twantrd
joe
mary
sue
john
So, list.txt is a list of items I want to keep. List1.txt contains all the items in a directory. Pretty much, I want to remove "sue" and "john". Not quite sure which direction I should take on how to write this bash script. If you guys can guide me a way, that would be great. Thanks!
Can you please post how it was done so others with the same question can read the answer in future.
Sure. The command 'comm' was the answer. It compares 2 sorted files line by line and describes what matches and what doesn't match in columns. Very simple to use.
The grep -v option didn't work. Well, I was checking each item in the list with a for loop and doing a grep -v against the other list and that gave me incorrect results.
If I did a 'grep -v twantrd list1.txt' it would show up "joe, mary, sue, john". However, that's incorrect as I just want to remove "sue" and "john". However, if you could provide the right way in doing a grep -v that works for my situation, that would be awesome. Thanks.
-twantrd
The grep -v option didn't work. Well, I was checking each item in the list with a for loop and doing a grep -v against the other list and that gave me incorrect results.
If I did a 'grep -v twantrd list1.txt' it would show up "joe, mary, sue, john". However, that's incorrect as I just want to remove "sue" and "john". However, if you could provide the right way in doing a grep -v that works for my situation, that would be awesome. Thanks.
-twantrd
i didn't understand the original post but this seems to be what you want:
Code:
schneidz@lq:/twantrd> cat list1.txt
twantrd
joe
mary
schneidz@lq:/twantrd> cat list2.txt
twantrd
joe
mary
sue
john
schneidz@lq:/twantrd> grep -f list1.txt list2.txt
twantrd
joe
mary
schneidz@lq:/twantrd>
sounds like you are deleting something that doesn't exist.
Correct, something that doesn't exist in 1 file but does in another. Therefore, I just want to remove the items that are unique. Anyhow, I got my answer. Thanks for the help!
Correct, something that doesn't exist in 1 file but does in another. Therefore, I just want to remove the items that are unique. Anyhow, I got my answer. Thanks for the help!
-twantrd
glad you got the answer by yourself. sometimes just asking for suggestions helps get the gears turning.
for anyone else wanting another suggestion:
try cat-ing both lists into 1 file then uniq -u the results.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.