Operating System - HP-UX
1755781 Members
4855 Online
108838 Solutions
New Discussion юеВ

question with join command..

 
SOLVED
Go to solution
amonamon
Regular Advisor

question with join command..

Hello..I have 2 files:

file1(pattern):

100018
100090
100143
100198
100346
100708
100961
100989
101037
101206
101543
101931
102306
102371
102504
102605
102640
102823
103025
103390

file2:

320346|006|12|11
901546|007|1|11
220054|007|12|11
100018|007|1|11
901411|007|12|11
380094|007|12|11
320046|006|12|11
901946|007|1|11
220854|007|12|11
310776|007|1|11
901611|007|12|11
380194|007|12|11

result should be:
100018

so basicly join 2 files and if record is listed in pattern(file1) do not print that record..

this is what I tryed so far:

join -t'|' -a1 file1 file2

but results are not OK..

then I also tryed this:

join -1 1 -2 1 -t'|' -a 1 file1 file2

still not OK??

can someone correct my misstake..

Thanks a lot..I am still beginner here but I try hard to make something..

cheers,
4 REPLIES 4
spex
Honored Contributor
Solution

Re: question with join command..

Hi,

From your example, it's not clear if you DO or DO NOT wish to print matching records in file2.

If you DO want matching records printed:
$ grep -F -f file1 file2
100018|007|1|11

If you wish to EXCLUDE matches in file2:
$ grep -F -v -f file1 file2
320346|006|12|11
901546|007|1|11
220054|007|12|11
901411|007|12|11
380094|007|12|11
320046|006|12|11
901946|007|1|11
220854|007|12|11
310776|007|1|11
901611|007|12|11
380194|007|12|11

PCS
James R. Ferguson
Acclaimed Contributor

Re: question with join command..

Hi:

If you truly want to use 'join' you need to sort your input files. See the manpages for 'join' for more information.

Regards!

...JRF...
amonamon
Regular Advisor

Re: question with join command..

Yes I learned man join and I realised and invoked sort -b file1

It works with command:

grep -F -f file1 file2

cool..but still I wanted to practice join command becouse I find it cool..

Thanks a lot
Sandman!
Honored Contributor

Re: question with join command..

The grep command is perfect for what you're trying to do. But if you wanted to practice the join(1) command you need to read the man page thoroughly as the second paragraph under DESCRIPTION section of the join(1) man page says:

>file1 and file2 must be sorted in increasing collating sequence (see >Environment Variables below) on the fields on which they are to be joined; >normally the first in each line.

So sort file1 and file2 before using the join(1) command on them, i.e.

# sort -k1,1 file1 > file1.out
# sort -t'|' -k1,1 file2 > file2.out
# join -1 1 -2 1 -t'|' -o 1.1 file1.out file2.out

~cheers