LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-28-2006, 06:22 PM   #1
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Rep: Reputation: 52
Not delete items in list


Hi there,

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!

-twantrd
 
Old 03-28-2006, 06:51 PM   #2
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Original Poster
Rep: Reputation: 52
I found the answer to my question. You guys can disregard this. Thanks.

-twantrd
 
Old 03-29-2006, 01:04 AM   #3
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
Can you please post how it was done so others with the same question can read the answer in future.
 
Old 03-29-2006, 12:12 PM   #4
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Original Poster
Rep: Reputation: 52
Quote:
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.

-twantrd
 
Old 03-29-2006, 01:10 PM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
also i think you can 'man grep' for option -v.
 
Old 03-29-2006, 03:18 PM   #6
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Original Poster
Rep: Reputation: 52
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
 
Old 03-30-2006, 10:09 AM   #7
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
It would be:
$ rm -fr $(comm -3 list.txt list1.txt)

<edit>
This also works:
diff list.txt list1.txt|sed -e'1d' -e 's/> //g'

Last edited by /bin/bash; 03-30-2006 at 10:27 AM.
 
Old 03-31-2006, 11:09 AM   #8
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Quote:
Originally Posted by 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>
 
Old 03-31-2006, 12:31 PM   #9
/bin/bash
Senior Member
 
Registered: Jul 2003
Location: Indiana
Distribution: Mandrake Slackware-current QNX4.25
Posts: 1,802

Rep: Reputation: 47
No I think he wants it to reply with only sue and john. Which are the 2 unique items which are only found in the one list.
 
Old 04-03-2006, 12:20 AM   #10
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Original Poster
Rep: Reputation: 52
Quote:
No I think he wants it to reply with only sue and john. Which are the 2 unique items which are only found in the one list.
Yup, that is correct. Thanks for the help guys.

-twantrd
 
Old 04-04-2006, 03:38 PM   #11
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
maybe:
Code:
schneidz@lq:/twantrd> grep -v -f list1.txt list2.txt
sue
john
else
i'm so confused
 
Old 04-04-2006, 03:41 PM   #12
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
maybe:
Code:
schneidz@lq:/twantrd> grep -v -f list1.txt list2.txt
sue
john
else
i'm so confused

Quote:
Originally Posted by twantrd
Trying to write a script that will delete items that are NOT in a file
sounds like you are deleting something that doesn't exist.
 
Old 04-04-2006, 10:52 PM   #13
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Original Poster
Rep: Reputation: 52
Quote:
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!

-twantrd
 
Old 05-02-2006, 12:58 PM   #14
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
Quote:
Originally Posted by twantrd
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.

~schneidz
 
Old 05-08-2006, 02:15 PM   #15
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440

Original Poster
Rep: Reputation: 52
Actually, that's not a bad idea as well. Thanks for the extra tip!

-twantrd
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Querying for data and porinting to a datagrid based on items in a list box mrobertson Programming 8 07-29-2005 01:23 PM
Take all posts that are news items out of the Zero Reply Threads list t3gah LQ Suggestions & Feedback 3 03-21-2005 08:38 AM
Alphabetizing a list of items not possible in text editor? jon_k Linux - Software 1 03-17-2005 12:00 AM
Delete menu items LosT ClusteR Linux - General 1 07-28-2003 05:28 PM
List controls for search items... Thymox LQ Suggestions & Feedback 2 05-21-2002 08:16 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration