compare second field


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting compare second field
# 1  
Old 06-08-2009
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  
Old 06-08-2009
can u post the o/p required ?..

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

which matches ???...
# 3  
Old 06-08-2009
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  
Old 06-08-2009
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  
Old 06-08-2009
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  
Old 06-08-2009
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  
Old 06-08-2009
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=
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help in splitting Sub Fields and compare with other field

Hi All, We are trying to pull out data from below table, the table contains four fields and out of which last two fields are having sub-fields with delimiter $, we want to identify number "1" position in the 3rd field and from 4th field need to extract the information from the same position. ... (4 Replies)
Discussion started by: rramkrishnas
4 Replies

2. Shell Programming and Scripting

Plz Help. Compare 2 files field by field and get the output in another file.

Hi Freinds, I have 2 files . one is source.txt and second one is target.txt. I want to keep source.txt as baseline and compare target.txt. please find the data in 2 files and Expected output. Source.txt 1|HYD|NAG|TRA|34.5|1234 2|CHE|ESW|DES|36.5|134 3|BAN|MEH|TRA|33.5|234... (5 Replies)
Discussion started by: i150371485
5 Replies

3. Shell Programming and Scripting

Compare two files Field by field and output the result in another file

Hi Friends, Need Help. I have file1.txt as File1.txt |123|A|7267|Hyder|Cross|Sell|7801 |995|A|7051|2008|Lunar|New|Year|Promotion|7801 |996|A|7022|Q108|Targ|Prospect|&|SSCC|Savings|Promo|7801 |997|A|7182|Q1|Feb-Apr|08|Credit|ITA|PA|SBA|Campaign|7801 File2.txt... (7 Replies)
Discussion started by: i150371485
7 Replies

4. Shell Programming and Scripting

Compare Field in Current Line with Field in Previous

Hi Guys I have the following file Essentially, I am trying to find the right awk/sed syntax in order to produce the following 3 distinct files from the file above: Basically, I want to print the lines of the file as long as the second field of the current line is equal to the... (9 Replies)
Discussion started by: moutaye
9 Replies

5. Programming

how to compare value of mysql field

#include <stdio.h> #include <fcntl.h> #include <signal.h> #include <unistd.h> #include <mysql.h> #include <string.h> #include <stdlib.h> #include <sys/time.h> #include <mysql.h> int main(int argc, char **argv) { MYSQL *conn; MYSQL_RES *result; MYSQL_ROW row; ... (0 Replies)
Discussion started by: slackman
0 Replies

6. Shell Programming and Scripting

compare two fields and get a third field

Hello, I'm trying to get a value based on a comparison of two fields, this is: file1 687.45 687.18 687.322 687.405 686.865 file 2 685 6.43 686 6.43 687 6.42 688 6.42 (3 Replies)
Discussion started by: Gery
3 Replies

7. Shell Programming and Scripting

AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

First, thanks for the help in previous posts... couldn't have gotten where I am now without it! So here is what I have, I use AWK to match $1 and $2 as 1 string in file1 to $1 and $2 as 1 string in file2. Now I'm wondering if I can extend this AWK command to incorporate the following: If $1... (4 Replies)
Discussion started by: right_coaster
4 Replies

8. Shell Programming and Scripting

Compare one field

Hello to every one: i have and archive like this: 5552952099|5552952099|VAHC600312UY9|VARGAS HERNANDEZ MA DEL CARMEN|METRO|LOMAS|CC|20||||900661026|1|METRO 5552952115|5552952949|AABF680818FT7|ABASOLO BONILLA FERDINAND|METRO|LOMAS|CC|20||||900671255|1|METRO... (6 Replies)
Discussion started by: xasky
6 Replies

9. Shell Programming and Scripting

AWK to compare two files for each field value

I have "n" files in directory A and "n" files in directory B. The files are expected to be the same with same data. Each file has 14 columns and "x" rows. Of the 14 column, 2 columns are to be considered as key identifiers. Based on this unique combination, I need to compare each field value... (2 Replies)
Discussion started by: Sangtha
2 Replies

10. Shell Programming and Scripting

How to compare a field value of a same column?

I have a file which has entries like as below, 10:29:07.32 GOOD 122 10:29:07.35 GOOD 124 10:29:09.97 BAD 122 10:29:09.97 BAD 124 10:29:18.66 ... (6 Replies)
Discussion started by: gobinath
6 Replies
Login or Register to Ask a Question