Find the answer to your Linux question:
Results 1 to 5 of 5
Hi, I hope somebody can help me with this. I need to check two files, if something is in the first file I need to either remove the line from ...
  1. #1
    Just Joined!
    Join Date
    Aug 2010
    Posts
    7

    Help comparing files

    Hi, I hope somebody can help me with this.

    I need to check two files, if something is in the first file I need to either remove the line from the second file or pipe it out to a new file.

    file1 will look like

    serial1
    serial2
    serial3

    file2 will look like

    serial1,cm1,mta1,key1
    serail2,cm2,mta2,key2
    serial3,cm3.mta3,key3
    serial4,cm4,mta4,key4
    serial4,cm5,mta5,key5

    What I need is to check is file1, if the serial exists in file1 and file2 then i need to remove the whole line from file2.

    file1 contains 259 serials and file2 contains 57000 lines so I should finish with 56741 lines in the output file or in file2.

    I tried sorting the files and using comm but I guess I must have dome something wrong.

    The output either ended up with more lines or no lines.

    I hope this is clear and thanks for any help.

  2. #2
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,097
    grep can do it
    Code:
    grep -f file1 -v file2 > file_result
    Probably you will want to be more precise:
    aka: grep for a number at the beginning of a line and/or the first field with ", " as delimiter.
    But the line above should be a start
    You must always face the curtain with a bow.

  3. #3
    Just Joined!
    Join Date
    Aug 2010
    Posts
    7
    Quote Originally Posted by Irithori View Post
    grep can do it
    Code:
    grep -f file1 -v file2 > file_result
    Probably you will want to be more precise:
    aka: grep for a number at the beginning of a line and/or the first field with ", " as delimiter.
    But the line above should be a start
    Thanks for the reply

    The output contained the same amount of lines, maybe i should have sorted first?

    Anyhow I got another response on the Gentoo forums which I'll post here as maybe it helps somebody else.

    Thanks again for the fast reply.

    #!/bin/awk
    # An example for helmutvandeshaft from the Gentoo forums.
    BEGIN {
    # Set the field separator to comma.
    FS = ",";

    # Slurp up all the serial numbers from file1.
    File1 = ARGV[1]; ARGV[1] = "";
    while ((getline <File1) > 0) {
    SernoList[$1] = 1;
    }
    }

    # Select all records where the serial number *does not* match the list.
    SernoList[$1] != 1

    #invoke with awk -f slurpey.awk file1 file2 > file3

  4. #4
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,097
    Hmm, the grep works.
    At least with the example files you provided (there are typos, btw).
    And also from what I understood of your description: the logic applies.

    Just a hunch: the real file1 is not a windows style textfile by any chance?
    Or are there more than serial numbers in every line? Even spaces will result in non-matching.
    You must always face the curtain with a bow.

  5. #5
    Just Joined!
    Join Date
    Aug 2010
    Posts
    7
    Hi

    The typos are my mistake, I could not paste the real contents as these are live emta details.

    As for the windows style textfile, I can't say, they were provided to me but I guess they would have been created on Solaris knowing the person that gave them to me, I only copied the files to Linux server to work on them.

    In file1, there are only serial numbers, in file2 there are serial numbers, cable modem mac, voip mac and the key, file 2 is , seperated.

    Regards

Posting Permissions

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