Operating System - HP-UX
1753500 Members
3457 Online
108794 Solutions
New Discussion юеВ

Re: Removing lines from one list from another list.

 
SOLVED
Go to solution
Patrick Ware_1
Super Advisor

Removing lines from one list from another list.

Hello,

I was wondering if there was an easy way to take lines from a single-column list, and remove them from a second single-column list. For example, I want to remove the contents of list 1 from list 2. How would I do this?

Contents of list 1:

server1a
server2b
server3c
server4a
server5qb
server6ocd


Contents of list 2"


server1a
server2b
server3c
server4a
server5qb
server6ocd
server7dgw
server8sg
server9be
server10zt
server11w
server12t
server13f
4 REPLIES 4
Patrick Ware_1
Super Advisor

Re: Removing lines from one list from another list.

I didn't realize it was this simple:

grep -v -f list1 list2
James R. Ferguson
Acclaimed Contributor
Solution

Re: Removing lines from one list from another list.

Hi:

Assuming that the file contents are each sorted:

# comm -13 file1 file2

...prints the lines from file2 not present in file1.

Regards!

...JRF...
Victor Fridyev
Honored Contributor

Re: Removing lines from one list from another list.

TMF=/tmp/$$
sort list1 list2 | uniq -c | awk '$1<2 {print $2}' > $TMF
sort $TMF list1 | uniq -c | awk '$1<2 {print $2}'
rm $TMF

HTH
Entities are not to be multiplied beyond necessity - RTFM
Dennis Handly
Acclaimed Contributor

Re: Removing lines from one list from another list.

>I didn't realize it was this simple: grep -v -f list1 list2

Yes, provided your files don't have 10s of thousands of lines.

Be careful you don't have substring matches that you don't want.
If server1 was in list1, it would remove server1a, server10zt, etc.

You can add -x to prevent this, provided you don't have any extra columns in list2.
There you may want to use -w.