?
Solved

outer join in unix with 2 files

Posted on 2003-02-20
8
Medium Priority
?
854 Views
Last Modified: 2013-12-13
I've 2 files :

file1
-----
496223224
497448063
497425608
497440502

file2
-----
A 20212 497448063
B 12141 497425608
C 18745 497440900

I'd like to have :

result_file (only record from file2 and not in file1)
-----------
C 18745 497440900



Thx in advance,
Yassin
0
Comment
Question by:yahrika
7 Comments
 
LVL 1

Expert Comment

by:skian
ID: 7986939

$ grep -v -f file1 file2

I think the -f option is not standard So only GNU grep
understands this.

Stephane
0
 

Author Comment

by:yahrika
ID: 7987353
This works great for a small file1 but if file2 is big

we have this error --> list too large

:(
0
 
LVL 2

Accepted Solution

by:
ellesd earned 60 total points
ID: 7987660
This script will do the trick:

cp file2 result_file
exec 3< file1
while read N <&3
do
  grep -v $N result_file > tmpfile$$
  mv tmpfile$$ result_file
done
rm -f tmpfile$$

The rm -f tmpfile$$ line isn't actually needed, but I usually include it as a safeguard.

Try it and let me know.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 5

Expert Comment

by:ecw
ID: 8169803
why not use join.  input must be sorted, so first,
  sort -n -o file1 file1
  sort -k 3n -o file2 file2
and then
  join -t ' ' -j1 1 -j2 3 -o 2.1,2.2,2.3 file1 file2
0
 

Expert Comment

by:dlstauf
ID: 8307495
For those of us not overly fond of the bourne/ksh shell:

foreach i (`cat file1`)
  grep $i file2 >&/dev/null
  if( $status != 0 ) echo $i >>result_file
end

or if file1 is large

split file1
foreach x (x??)
  foreach i (`cat $x`)
    grep $i file2 >&/dev/null
    if( $status != 0 ) echo $i >>result_file
  end
  rm $x
end
0
 
LVL 5

Expert Comment

by:ecw
ID: 8309770
Yeah, but it's hard to find aanyone who recommends programming in csh.  It's fine for "interactive" work but an abomination when one wants to do serious processing.
0
 

Expert Comment

by:greentorch
ID: 8320492
Like this?
join -1 1 -2 3 file1 file2 -v 2
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

579 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question