?
Solved

outer join in unix with 2 files

Posted on 2003-02-20
8
Medium Priority
?
841 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
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 …

777 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