Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Comparing 2 files

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 03-26-2013
nua7 nua7 is offline
Registered User
 
Join Date: Mar 2008
Last Activity: 30 June 2015, 9:23 AM EDT
Location: /bin/sh
Posts: 433
Thanks: 26
Thanked 4 Times in 4 Posts
Comparing 2 files

Hi,
I have two files in the following format.
File 1

Code:
S00002583|NORFO|0002.20|MR|015542324A||BR|STD|201206|015542324A||E
S00004144|MIDDL|0014.90|MR|017120472D||VR|STD|201206|017120472D||E
S00005307|PLYMO|0002.20|MR|026187410A||P|STD|201206|026187410A||E
S00006040|SUFFO|0002.20|MR|012227071A||R|STD|201206|012227071A||E
S00017646|ESSEX|0009.60|MR|019036684A||BR|STD|201206|019036684A||E
S00019378|MIDDL|0013.40|MR|032072554A||VR|STD|201206|032072554A||E
S00025106|SUFFO|0009.60|MR|030201632A||R|STD|201206|030201632A||E
S00026461|MIDDL|-0006.10|MR|028447503A||R3|STD|201106|028447503A||E
S00026462|MIDDL|-0006.10|MR|028447502A||R3|STD|201107|028447502A||E
S00029844|ESSEX|0002.20|MR|030228184A||VR|STD|201206|030228184A||E
S00030343|ESSEX|0002.20|MR|033185658B||R|STD|201206|033185658B||E
S00037588|ESSEX|0007.50|MR|016241003A||R|STD|201206|016241003A||E
S00046838|MIDDL|0002.20|MR|025220396A||RK|STD|201206|025220396A||E
S00046948|PLYMO|0002.20|MR|012228991A||P|STD|201206|012228991A||E
S00047201|ESSEX|0002.20|MR|030228313A||R|STD|201206|030228313A||E
S00047205|MIDDL|-0007.30|MR|033220633A||P|STD|200907|033220633A||E
S00047785|MIDDL|0014.90|MR|026243695A||R|STD|201206|026243695A||E
S00048005|MIDDL|-0017.10|MR|022078063A||VR|STD|201205|022078063A||E
S00050356|ESSEX|0009.60|MR|200223079A||BR|STD|201206|200223079A||E
S00050497|ESSEX|0002.20|MR|024229648A||VR|STD|201206|024229648A||E
S00051590|NORFO|0009.60|MR|016242468A||R|STD|201206|016242468A||E

File 2 :

Code:
S00001006|0|20120731|32|MR|201207|E
S00001023|0|20090731|0|MR|200907|E
S00001028|0|20110131|0|MR|201101|E
S00001034|0|20110131|0|MR|201101|E
S00001042|0|20090431|0|MR|200904|E
S00001044|0|20100331|0|MR|201003|E
S00001046|0|20110731|0|MR|201107|E
S00001054|0|20121031|654.1|MR|201210|E
S00001058|0|20121031|625.8|MR|201210|E
S00001149|0|20121031|409.8|MR|201210|E
S00001153|0|20121031|654.1|MR|201210|E
S00001156|0|20121031|654.1|MR|201210|E
S00001167|0|20121031|654.1|MR|201210|E
S00001173|0|20060331|117|MR|200603|E
S00001181|0|20080431|-21.5|MR|200804|E
S00001182|0|20070431|404|MR|200704|E
S00001184|0|20110631|159.9|MR|201106|E
S00001196|0|20080831|-22|MR|200808|E
S00001231|0|20111131|759.4|MR|201111|E
S00029844|0|20090731|0|MR|200907|E
S00030343|0|20110131|0|MR|201101|E
S00037588|0|20110131|0|MR|201101|E
S00046838|0|20090431|0|MR|200904|E
S00046948|0|20100331|0|MR|201003|E
S00047201|0|20110731|0|MR|201107|E
S00047205|0|20121031|654.1|MR|201210|E
S00047785|0|20121031|625.8|MR|201210|E
S00048005|0|20121031|409.8|MR|201210|E
S00050356|0|20121031|654.1|MR|201210|E
S00050497|0|20121031|654.1|MR|201210|E
S00051590|0|20121031|654.1|MR|201210|E
S00053315|0|20060331|117|MR|200603|E
S00054151|0|20080431|-21.5|MR|200804|E
S00060160|0|20070431|404|MR|200704|E
S00046948|0|20110631|159.9|MR|201106|E
S00047201|0|20080831|-22|MR|200808|E
S00037588|0|20111131|759.4|MR|201111|E

I need to compare first columns of both the files and print only matching lines from file 2.

I tried the following code but doesn't help.


Code:
awk -F "|" '{A[$1,$1]=1;next} A[$1,$1]' FILE2 FILE1 > tst2

Desired output :

Code:
S00046948|0|20110631|159.9|MR|201106|E
S00047201|0|20080831|-22|MR|200808|E
S00037588|0|20111131|759.4|MR|201111|E

Any help is appreciated.
Sponsored Links
    #2  
Old Unix and Linux 03-26-2013
joeyg's Unix or Linux Image
joeyg joeyg is offline Forum Staff  
modérateur
 
Join Date: Dec 2007
Last Activity: 22 June 2015, 10:58 AM EDT
Location: Within two miles of a Dunkin donuts.
Posts: 2,356
Thanks: 85
Thanked 163 Times in 149 Posts
Just thinking...

Gotta re-load unix on this PC, so this is all theory...


Code:
cut -d"|" -f1 <file1 >file1key
grep -f file1key <file2

The Following User Says Thank You to joeyg For This Useful Post:
nua7 (03-26-2013)
Sponsored Links
    #3  
Old Unix and Linux 03-26-2013
Yoda's Unix or Linux Image
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 30 June 2015, 5:05 PM EDT
Location: Galactic Empire
Posts: 3,408
Thanks: 240
Thanked 1,216 Times in 1,142 Posts

Code:
join -t"|" -1 1 -2 1 -o 1.1 1.2 1.3 1.4 1.5 1.6 1.7 file2 file1

The Following User Says Thank You to Yoda For This Useful Post:
nua7 (03-26-2013)
    #4  
Old Unix and Linux 03-26-2013
sabercats sabercats is offline
Registered User
 
Join Date: Feb 2006
Last Activity: 25 June 2015, 7:37 PM EDT
Posts: 130
Thanks: 1
Thanked 0 Times in 0 Posts
I did it before and here what i will use

Code:
awk -F\| 'NR==FNR{a[$1]++;next} (a[$1])' FILE1 FILE2 > FILE3.txt

Sponsored Links
    #5  
Old Unix and Linux 03-26-2013
morbid_angel morbid_angel is offline
Registered User
 
Join Date: Nov 2009
Last Activity: 27 January 2015, 2:20 PM EST
Posts: 33
Thanks: 2
Thanked 0 Times in 0 Posts
Using fgrep


Code:
cat file1 |cut -d"|" -f1  >Newfile_1
fgrep -f Newfile_1 file2 >Newfile


Last edited by Franklin52; 03-27-2013 at 03:51 AM.. Reason: Please use code tags for data and code samples
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to find duplicates contents in a files by comparing other files? Venkatesh1 UNIX for Advanced & Expert Users 7 07-27-2012 09:01 AM
Comparing the matches in two files using awk when both files have their own field separators asyed Shell Programming and Scripting 2 09-24-2011 05:46 AM
Need help comparing two files and deleting some things in those files! linuxkid Shell Programming and Scripting 24 07-28-2010 05:32 AM
comparing 2 files lassimanji Shell Programming and Scripting 7 05-02-2009 09:40 AM
comparing shadow files with real files terrym UNIX for Advanced & Expert Users 4 02-09-2007 01:38 AM



All times are GMT -4. The time now is 05:25 PM.