The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Find top N values for field X based on field Y's value FrancoisCN Shell Programming and Scripting 1 05-29-2009 10:57 AM
How to compare a field value of a same column? gobinath Shell Programming and Scripting 6 05-06-2009 02:39 AM
Compare two arrays in sh or compare two fields rijeshpp Shell Programming and Scripting 0 10-31-2007 02:47 AM
Compare dates in a field and print the latest date row cvkishore Shell Programming and Scripting 1 08-04-2007 08:58 AM
Moving Part of a field to another field using AWK rjsha1 Shell Programming and Scripting 5 08-04-2006 06:39 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-08-2009
kaprus kaprus is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 19
compare second field

Hi,

I have two files, mentioning below
1st file:
919030055000,1
919030055000,3
919030055000,7
919030155000,1
919030155000,3
919031055000,1
919031055000,8
919031155000,1
919031155000,8
919036055000,3
919036055000,7
919036155000,3
919036155000,7
919040055000,1
919040055005,7
919045123243,13

2nd file:
919030,1
919031,8
919036,3
919040,7
919045,13

I need to compare 2nd field of each entry in 1st file with the 2nd field of 2nd file with corresponding entry.
Kindly suggest me any command or something else in Unix.

Result required : COUNT for both fields matchs = 5

Thanks for support.
  #2 (permalink)  
Old 06-08-2009
panyam panyam is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2008
Posts: 474
can u post the o/p required ?..


Code:
Result required : COUNT for both fields matchs = 5

which matches ???...
  #3 (permalink)  
Old 06-08-2009
kaprus kaprus is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 19
I have to count the matches field from both files:
matching fields from 1st file are:

919030055000,1
919031055000,8
919036055000,3
919040055000,7
919045123243,13

corresponded to 2nd file entry.

Hence count is 5.

Thanks for your kind attention.
  #4 (permalink)  
Old 06-08-2009
panyam panyam is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2008
Posts: 474
Still not proper ..


Code:
919030055000,1
919031055000,8
919036055000,3
919040055000,7
919045123243,13

corresponded to 2nd file entry.

Where as your second file contains :

Code:
2nd file:
919030,1
919031,8
919036,3
919040,7
919045,13

where the match is happening ?...

Y not
Code:
         919030155000,1
         919031155000,8
         ...........
         .......

From the First file ?...
  #5 (permalink)  
Old 06-08-2009
kaprus kaprus is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 19
I have to count the matches field from both files:
matching fields from 1st file are:

919030055000,1
919030055000,3
919030055000,7
919030155000,1
919030155000,3
919031055000,1
919031055000,8
919031155000,1
919031155000,8
919036055000,3
919036055000,7
919036155000,3
919036155000,7
919040055000,1
919040055005,7
919045123243,13

corresponded to 2nd file entry.
919030,1
919031,8
919036,3
919040,7
919045,13

result is
919030055000,1
919031055000,8
919036055000,3
919040055000,7
919045123243,13
Count=5

i did work :
cat Second_FILE.txt | while read myLine
do
myWord1=`echo $myLine|awk -F "," '{print $1}'`
myWord2=`echo $myLine|awk -F "," '{print $2}'`

myWord4=`grep ^$myWord1 FIRST.txt | awk -F "," '{print $2}'`
################### Comparing 2nd field of 1st file with 2nd file ##############
if [ $myWord2 -eq $myWord4 ];then
hplmn=`expr $hplmn + 1`
echo $hplmn >> NUMBER.txt
fi

echo "COUNT for both fields matchs =" $hplmn
done
But it is not work properly;
  #6 (permalink)  
Old 06-08-2009
Franklin52 Franklin52 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2007
Posts: 4,350
Try this:


Code:
awk -F, '
NR==FNR{a[$1$2];next}
substr($1,1,6)$2 in a{print;s++;delete a[substr($1,1,6)$2]}
END{print "Count="s}
' file2 file1

Use nawk or /usr/xpg4/bin/awk on Solaris.
  #7 (permalink)  
Old 06-08-2009
kaprus kaprus is offline
Registered User
  
 

Join Date: Feb 2009
Posts: 19
Output generated by given code:

awk -F, '
> NR==FNR{a[$1$2];next}
> substr($1,1,6)$2 in a{print;s++;delete a[substr($1,1,6)$2]}
> END{print "Count="s}
> ' DATAFILE.txt CONFIG.txt
Count=
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:50 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0