If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > to find a match between two files

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 05-28-04, 11:15
skd skd is offline
Registered User
 
Join Date: Sep 2003
Posts: 71
to find a match between two files

I am trying to read two files and want to display

if (field1 of file2) == (field2 of file1) then
echo (file2:field1,2,3) (file1:field1)
endif

=================================================


fille1 : clist
DC DOBS DOBSON
NE NXTP NEXTEL
XE NXTL NEXTEL
SP SPPCS SPPCS
SP SPRNT SPRINT
VW VZW VZW
VZ VERIZ VERIZON
TG TGTEL TGTEL
TT TRTEL TRITEL
WW WWPCS WWPCS
EK EKN EKN
TM TMOB TMOBILE
AW ACIW ACIW

fille2 :ilist
aciw jefferson ne
aciw saline ne
aciw clay ne
aciw gage ne
aciw nemaha ne
wwpcs saline ne
wwpcs jefferson ne
wwpcs gage ne
wwpcs nemaha ne
nxtp gage ne
trtel hardin ky
trtel barren ky
nxtp boyd ky
nxtp pulaski ky
nxtp crawford pa
nxtp chautauqua ny
vzw chautauqua ny
nxtp woodford ky
nxtp rowan ky
nxtp garrard ky
nxtp jessamine ky
nxtp laurel ky
nxtp allen ky
nxtp hardin ky
nxtp taylor ky
dobs crawford pa
vzw crawford pa
attws crawford pa
dobs adair ky
dobs garrard ky
dobs laurel ky
dobs green ky
dobs chautauqua ny
nxtl fayette ky
sppcs fayette ky
attws fayette ky
attws woodford ky
attws boyd ky
attws carter ky
attws jessamine ky
aciw boyd ky
aciw carter ky
nxtp carter ky
sppcs chautauqua ny
sppcs hardin ky

=================================================


// here is how i have managed to display, is therebetter way to do this ? may be in awk.or else..
cat findmatch.sh
================
#set -xv
while read x y z
X=`echo $x|tr '[a-z]' '[A-Z]'`
do
#
while read c1 c2 c3
do
if [ "$X"="$c2" ]
then
echo $z $y $x $c2|tr '[a-z]' '[A-Z]'
else
break;
fi
done < clist
#
done < ilist

also extra spacing between if conditions creates problem here , does this only happen in korn shell?
Reply With Quote
  #2 (permalink)  
Old 05-28-04, 11:34
iaguigon iaguigon is offline
Registered User
 
Join Date: May 2004
Location: Barcelona, Spain
Posts: 54
You can read clist and ilist at the same time, thus not reading clist more than once. You should sort both files by the compared fields.
There's a thread that covers this issue called "Merging files in a single one", dated May 25th. You can follow that for help!

Also, if you have access to MySQL, you can import both files into two tables and let sql do work for you. I suppose this is slower, anyway.

Hope this helps you

Regards

Iņaki
Reply With Quote
  #3 (permalink)  
Old 05-28-04, 14:41
skd skd is offline
Registered User
 
Join Date: Sep 2003
Posts: 71
Thanks for quick reply. i will see the post.
i have these files on HP-unix and i am not using any DBMS.
Reply With Quote
  #4 (permalink)  
Old 05-28-04, 17:09
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
something like this in awk

nawk -f skd.awk file1 file2

Code:
FNR == NR {
  f1[toupper($2)]=$1
  next;
}

toupper($1) in f1 { print $0, f1[toupper($1)] }
Reply With Quote
  #5 (permalink)  
Old 06-01-04, 14:03
skd skd is offline
Registered User
 
Join Date: Sep 2003
Posts: 71
Thank you vgresh99.

I am not much familiar with awk. what is file sdk.awk for in your solution?
Reply With Quote
  #6 (permalink)  
Old 06-01-04, 15:21
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
sdk.awk is a file containing the 'Code' in the post.
Reply With Quote
  #7 (permalink)  
Old 06-02-04, 11:02
skd skd is offline
Registered User
 
Join Date: Sep 2003
Posts: 71
duh??? me...

Is nawk system specific?
Reply With Quote
  #8 (permalink)  
Old 06-02-04, 14:18
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
might be. What OS are you on?
Try 'awk' [or gawk] if you don't have 'nawk'.
Reply With Quote
  #9 (permalink)  
Old 06-02-04, 16:07
skd skd is offline
Registered User
 
Join Date: Sep 2003
Posts: 71
BTW i am using HP-UX so only awk here.
Thanx
Reply With Quote
  #10 (permalink)  
Old 06-02-04, 16:09
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
ok, so try awk
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On