match file - Unix

This is a discussion on match file - Unix ; I have a sorted file1 that I want to make sure the keys in file1 are taken out from file2. Is there a command that can handle that?...

+ Reply to Thread
Results 1 to 7 of 7

Thread: match file

  1. match file


    I have a sorted file1 that I want to make sure the keys in file1 are
    taken out from file2.
    Is there a command that can handle that?


  2. Re: match file

    2006-11-8, 10:30(-08), rogv24@yahoo.com:
    >
    > I have a sorted file1 that I want to make sure the keys in file1 are
    > taken out from file2.
    > Is there a command that can handle that?


    I'm not quite sure what you mean, but you may want to have a
    look at the join, comm and uniq commands.

    --
    Stéphane

  3. Re: match file

    In <1163010638.348908.17170@b28g2000cwb.googlegroups.c om> rogv24@yahoo.com writes:

    > I have a sorted file1 that I want to make sure the keys in file1 are
    > taken out from file2.
    > Is there a command that can handle that?


    There is not a single command that will do that, no.

    However, there are commands which, when used together, can probably do
    what you want.

    For example, you could probably use "awk" to extract the keys from each
    file, then use "comm" or "diff" to compare the two key lists, and finally
    use "sed" or "grep" to make a new copy of file2 which contains only the
    keys you want.

    --
    John Gordon "... What with you being his parents and all,
    gordon@panix.com I think that you could be trusted not to shaft
    him." -- Robert Chang, rec.games.board


  4. Re: match file

    John Gordon wrote:
    > In <1163010638.348908.17170@b28g2000cwb.googlegroups.c om> rogv24@yahoo.com writes:
    >
    >
    >>I have a sorted file1 that I want to make sure the keys in file1 are
    >>taken out from file2.
    >>Is there a command that can handle that?

    >
    >
    > There is not a single command that will do that, no.
    >
    > However, there are commands which, when used together, can probably do
    > what you want.
    >
    > For example, you could probably use "awk" to extract the keys from each
    > file, then use "comm" or "diff" to compare the two key lists, and finally
    > use "sed" or "grep" to make a new copy of file2 which contains only the
    > keys you want.
    >


    awk can do it on it's own. It sounds like the OP want's something like this:

    awk 'NR==FNR{keys[$0];next}!($0 in keys)' file1 file2

    That'll delete every line from file2 that exists in file 1. If the keys
    exist in specific fields of each line in each file, use $1, $2, etc.
    instead of $0 to identify the key fields, e.g. if the key field in fil1
    is field 3, and the key field in file2 is field 7, it'd be:

    awk 'NR==FNR{keys[$3];next}!($7 in keys)' file1 file2

    Regards,

    Ed.

  5. Re: match file



    Thanks for the info:

    I used comm -1 file1 file2 and it works fine.


    Stephane CHAZELAS wrote:
    > 2006-11-8, 10:30(-08), rogv24@yahoo.com:
    > >
    > > I have a sorted file1 that I want to make sure the keys in file1 are
    > > taken out from file2.
    > > Is there a command that can handle that?

    >
    > I'm not quite sure what you mean, but you may want to have a
    > look at the join, comm and uniq commands.
    >
    > --
    > Stéphane



  6. Re: match file



    Thanks for the info:

    I used comm -1 file1 file2 and it works fine.


    Stephane CHAZELAS wrote:
    > 2006-11-8, 10:30(-08), rogv24@yahoo.com:
    > >
    > > I have a sorted file1 that I want to make sure the keys in file1 are
    > > taken out from file2.
    > > Is there a command that can handle that?

    >
    > I'm not quite sure what you mean, but you may want to have a
    > look at the join, comm and uniq commands.
    >
    > --
    > Stéphane



  7. Re: match file



    Thanks for the info:

    I used comm -1 file1 file2 and it works fine.


    Stephane CHAZELAS wrote:
    > 2006-11-8, 10:30(-08), rogv24@yahoo.com:
    > >
    > > I have a sorted file1 that I want to make sure the keys in file1 are
    > > taken out from file2.
    > > Is there a command that can handle that?

    >
    > I'm not quite sure what you mean, but you may want to have a
    > look at the join, comm and uniq commands.
    >
    > --
    > Stéphane



+ Reply to Thread