Find the answer to your Linux question:
Results 1 to 3 of 3
I've got two files. They both contain package names. Is there any way I can go through the package list on one file, and search to see if each package ...
  1. #1
    Just Joined!
    Join Date
    Nov 2009
    Posts
    13

    Bash query

    I've got two files. They both contain package names. Is there any way I can go through the package list on one file, and search to see if each package exists in the other file? What I'd want to do, is if the package name is found in the the main file, then go to the next line. If its not found then print that package name to another file.

    I know you can use diff, but it doesn't seem to be that straight forward. As I understand it diff searches line by line, so if line one doesn't match line one in another file, then it prints it out. That's not exactly what I want.

    I just really need an easy way to filter out the additional packages that exist on a new server. If I have a list of packages that aren't on the original server, then i can just delete them.

    Not sure if I've made any sense but there must be a quicker way to do what i need to. It would take me ages to scan manually through the package names in each list, and highlight the ones i dont need.

  2. #2
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,097
    What about this:

    Code:
    sort list1 |uniq > sorted_list1
    sort list2 |uniq > sorted_list2
    
    diff sorted_list1 sorted_list2
    You must always face the curtain with a bow.

  3. #3
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.
    Code:
           comm [OPTION]... FILE1 FILE2
    
    DESCRIPTION
           Compare sorted files FILE1 and FILE2 line by line.
    
           With  no  options,  produce  three-column  output.  Column one contains
           lines unique to FILE1, column two contains lines unique to  FILE2,  and
           column three contains lines common to both files.
    
    -- excerpt from man comm, q.v.
    Another method would be:
    Code:
    sort file1 file2 | uniq -u
    note that this will list items that are unique in each file, but your description indicated that one would be a super-set of the other, i.e not of wildly different content.

    If the files are not too long:
    Code:
    grep -v -f file1 file2
    will avoid the sort ... cheers, drl
    Last edited by drl; 04-16-2010 at 10:37 AM. Reason: ( Edit 1: look for unique items, not duplicates. )
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...