[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] unmatched
From: |
Dennis Williamson |
Subject: |
Re: [Help-bash] unmatched |
Date: |
Mon, 5 Sep 2016 12:15:36 -0500 |
On Sat, Sep 3, 2016 at 5:57 PM, Val Krem <address@hidden> wrote:
> Hi all,
>
> I have two files file1 and file2.
>
> file1
> 4
> 5
>
> File2
> ID y1 y2 y3
>
> 125 4 0 465
> 126 4 2001 12
> 127 4 2002 40
> 128 4 2016 1
> 135 5 0 465
> 136 5 2001 12
> 137 5 2002 40
> 138 6 2016 1
> 139 6 2017 2
>
>
> My aim to match the two files (file1 and file2) and list the unmatched
> lines from file 2.
>
> I matching file1(has only one field) with field 2 (Y1) of file2 and to
> get the output that do not match.
> here
>
> file 3
>
> 138 6 2016 1
> 139 6 2017 2
>
>
> I used the following to get the matching lines between the two files
>
>
> awk 'NR==FNR{c[$1]++;next};c[$2] >0' file1 file2
>
>
> Is it possible to get the negative of this teh above line? i.e., to get
> the unmatched lines
>
>
> Thank you in advance
>
>
join is part of GNU coreutils and is also present on many Unix-like systems.
join -1 1 -2 2 -v 2 file1 file2 > file3
This says join field 1 of file1 with field2 of file2 and show the unmatched
lines of file2. The files need to be sorted (which yours already are). If
they werent already sorted, then one way to do it would be:
join -1 1 -2 2 -v 2 <(sort file1) <(sort file2)
See the man page for more information.
--
Visit serverfault.com to get your system administration questions answered.