How to compare two column using awk?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to compare two column using awk?
# 1  
Old 09-11-2017
How to compare two column using awk?

Hi team,

I have below sample file. It has 4 columns. I want awk script to compare column2 and column4 row by row and print result in new column5

If value matches then result should be MATCHED
if not then result should be NOT MATCHED

Input file as below

Code:
UDC_MSISDN,UDC_NPREFIX STG_MSISDN,STG_IMSI_NPREFIX
466000000000000,1545 466000000000000,820011902457295
466000000100,1545 466000000100,820011001002929
466000000101,1545 466000000101,820011001002927
466000000201,1545 466000000201,820011001002951
466000000300,1545 466000000300,820011001002950
466000000301,1545 466000000301,820011001002955
466500066972,1479 466500066972,1479
466500066977,1735 466500066977,1735
466500066980,1479 466500066980,1479
466500066981,1479 466500066981,1479
466500066987,1479 466500066987,1479
466500067002,1479 466500067002,1479
466500067006,1479 466500067006,1479

Code:
UDC_MSISDN,UDC_NPREFIX STG_MSISDN,STG_IMSI_NPREFIX
466000000000000,1545 466000000000000,820011902457295 NOTMATCHED
466000000100,1545 466000000100,820011001002929 NOTMATCHED
466000000101,1545 466000000101,820011001002927 NOTMATCHED
466000000201,1545 466000000201,820011001002951 NOTMATCHED
466000000300,1545 466000000300,820011001002950 NOTMATCHED
466000000301,1545 466000000301,820011001002955 NOTMATCHED
466500066972,1479 466500066972,1479 MATCHED
466500066977,1735 466500066977,1735 MATCHED
466500066980,1479 466500066980,1479 MATCHED
466500066981,1479 466500066981,1479 MATCHED
466500066987,1479 466500066987,1479 MATCHED
466500067002,1479 466500067002,1479 MATCHED
466500067006,1479 466500067006,1479 MATCHED

Thanks in advance

Shanul

Last edited by hicksd8; 04-26-2020 at 08:26 AM..
# 2  
Old 09-11-2017
Hello shanul karim,

I have a few to questions pose in response first:-
  • Is this homework/assignment? There are specific forums for these.
  • What have you tried so far?
  • What output/errors do you get?
  • What OS and version are you using?
  • What are your preferred tools, if not just awk? (C, shell, perl, awk, etc.)
  • What logical process have you considered? (to help steer us to follow what you are trying to achieve)
Most importantly, What have you tried so far?

There are probably many ways to achieve most tasks, so giving us an idea of your style and thoughts will help us guide you to an answer most suitable to you so you can adjust it to suit your needs in future.


We're all here to learn and getting the relevant information will help us all.


Kind regards,
Robin
This User Gave Thanks to rbatte1 For This Post:
# 3  
Old 09-11-2017
hi rbatt,

I have tried this below code

Code:
awk '{if($2=$4) print $5="MATCHED" ; else print $5="NOTMATCHED"}' UDC_STG_COMP > UDC_STG_COMP1

But output is not as expected.

Coming to your question.

Is this homework/assignment? There are specific forums for these.

Code:
No homework. Actually i have two big files and i have parse them into one single file using below awk command. But now stuck at this point where i need to match these two column. its part of almost done assignment. with its solution this will done.

Code:
awk '
FNR==NR {a[$1] = $0
          next
         }

         {print $0, a[$1]?a[$1]:"NA"
         }
 ' FS=, STG3_DATA UDC_DATA > UDC_STG_COMP

What have you tried so far?

i have tried this below

Code:
awk '{if($2=$4) print $5="MATCHED" ; else print $5="NOTMATCHED"}' UDC_STG_COMP > UDC_STG_COMP1

What output/errors do you get?

The output is not as desired..

Code:
$ awk '{if($2=$4) print $5="MATCHED" ; else print $5="NOTMATCHED"}' UDC_STG_COMP > UDC_STG_COMP1

eshaqur@IN00106004 ~/MNP_PRE_CUTOVER_AUDIT
$ head -n 10 UDC_STG_COMP1
NOTMATCHED
NOTMATCHED
NOTMATCHED
NOTMATCHED
NOTMATCHED
NOTMATCHED
NOTMATCHED

What OS and version are you using?

Code:
Windows and trying this awk script in linux environment

What are your preferred tools, if not just awk? (C, shell, perl, awk, etc.)

Code:
awk will be good because i am learning same as well.. if not shell,perl will be ok.

thanks for your showing interest and helping me. Looking forward for solution if you can provide me. it will be really greatful

Regards,
Shanul
# 4  
Old 09-11-2017
Try
Code:
awk -F"[ ,]" '{print $0, ($2==$4?_:"NOT") "MATCHED"}' file
UDC_MSISDN,UDC_NPREFIX STG_MSISDN,STG_IMSI_NPREFIX NOTMATCHED
466000000000000,1545 466000000000000,820011902457295 NOTMATCHED
466000000100,1545 466000000100,820011001002929 NOTMATCHED
466000000101,1545 466000000101,820011001002927 NOTMATCHED
466000000201,1545 466000000201,820011001002951 NOTMATCHED
466000000300,1545 466000000300,820011001002950 NOTMATCHED
466000000301,1545 466000000301,820011001002955 NOTMATCHED
466500066972,1479 466500066972,1479 MATCHED
466500066977,1735 466500066977,1735 MATCHED
466500066980,1479 466500066980,1479 MATCHED
.
.
.


Last edited by hicksd8; 04-26-2020 at 08:27 AM..
This User Gave Thanks to RudiC For This Post:
# 5  
Old 09-11-2017
Really thanks a lot for this help.

Also, If you can explain this code. Then this learning will really help me in future.
# 6  
Old 09-11-2017
Code:
awk -F"[ ,]" '                  # set the field separator to space OR comma, so you really have 4 fields 
{print $0,                      # print the line unmodified PLUS the OFS character
($2==$4?_:"NOT")                # ternary (or conditional) assignment: if the two fields are equal,
                                # don't print anything (empty undefined variable "_"), else print "NOT"
 "MATCHED"}                     # (unconditionally) print "MATCHED"
' file                          # file name to work upon

This User Gave Thanks to RudiC For This Post:
# 7  
Old 09-11-2017
Hello shanul karim,

Welcome to forums, I hope you will enjoy learning and sharing knowledge here. Could you please try following and let me know if this helps you.
Code:
awk -F, '{split($2,a," ");if(a[1]==$3){print $0,"Matched"} else {print $0,"Non-Matched"}}'   Input_file

Output will be as follows.
Code:
UDC_MSISDN,UDC_NPREFIX STG_MSISDN,STG_IMSI_NPREFIX Non-Matched
466000000000000,1545 466000000000000,820011902457295 Non-Matched
466000000100,1545 466000000100,820011001002929 Non-Matched
466000000101,1545 466000000101,820011001002927 Non-Matched
466000000201,1545 466000000201,820011001002951 Non-Matched
466000000300,1545 466000000300,820011001002950 Non-Matched
466000000301,1545 466000000301,820011001002955 Non-Matched
466500066972,1479 466500066972,1479 Matched
466500066977,1735 466500066977,1735 Matched
466500066980,1479 466500066980,1479 Matched
466500066981,1479 466500066981,1479 Matched
466500066987,1479 466500066987,1479 Matched
466500067002,1479 466500067002,1479 Matched
466500067006,1479 466500067006,1479 Matched

Thanks,
R. Singh

Last edited by hicksd8; 04-26-2020 at 08:29 AM..
This User Gave Thanks to RavinderSingh13 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need awk or Shell script to compare Column-1 of two different CSV files and print if column-1 matche

Example: I have files in below format file 1: zxc,133,joe@example.com cst,222,xyz@example1.com File 2 Contains: hxd hcd jws zxc cst File 1 has 50000 lines and file 2 has around 30000 lines : Expected Output has to be : hxd hcd jws (5 Replies)
Discussion started by: TestPractice
5 Replies

2. Shell Programming and Scripting

Need help on awk to compare only integer on particular column

Hi, 0.23 2.94% 0.00 0.00% 17.8G 55.7% 19.6G 40.9% 630 0.00% 0.06 0.77% - - 7524M 22.9% 15.6G 32.6% - - From the above sample output. I need to compare whether the 6th field is more than 10G..if so print the entire line. Here the 6th field is memory TIA (5 Replies)
Discussion started by: Sumanthsv
5 Replies

3. UNIX for Dummies Questions & Answers

awk command to compare files by column

So I have this issue. I have 4 files. the first one is the master file who has all possible combinations: file 1 - a - b - c - d - e the other three have some of the letters and a number instead of - for example file 2 34 a 5 c file 3 10 b 12 ... (3 Replies)
Discussion started by: Quijotes
3 Replies

4. Shell Programming and Scripting

How to compare the values of a column in a same file using awk?

Dear Unix experts, I have got a file where I would like to compare the values of second column if first column is same in such a way that the difference between the values is >50. If not, I would like to discard both values. For example, my input file looks like - comp275_c0_seq2 73... (7 Replies)
Discussion started by: utritala
7 Replies

5. Shell Programming and Scripting

Compare the second column of a file with the second column of another in awk

Hi, I know that this topic has been discussed in the past and I've tried to follow all the guidelines. Anyhow, I following describe my problem. I have a file (file1 , no. records = 67) containing pairs of IP addresses as follows (with single space as delimiter between the fields): example... (5 Replies)
Discussion started by: amarn
5 Replies

6. Shell Programming and Scripting

awk compare column n replace with in one file

hi Friends need to compare columns in one file where the data looks like below laptop,IBM phone,samsung car,rental user1,laptop user2,laptop user3,phone want to get output as laptop,IBM phone,samsung car,rental user1,IBM user2,IBM user3,samsung need to seach $2 in array of $1 and... (4 Replies)
Discussion started by: arun1401
4 Replies

7. Shell Programming and Scripting

awk arrays - compare value in second column to variable

Hello, I am trying to redirect files to a directory by using a config file. The config files is as such: xxxxxx,ID,PathToDirectory xxxxxx,ID2,PathToDirectory2 and so on... I have a variable that should match one of these IDs. I want to load this config file into an awk array, and... (2 Replies)
Discussion started by: jrfiol
2 Replies

8. Shell Programming and Scripting

awk compare column with date format

I have this code to compare columns 1 and 10 between file1 and file 2 and give me all records that match column 1 but dont match column 10 However column 10 is date format mm/dd/yy and awk cant read it and compare ...i tried awk < file1 -F~ '{print $10}' and it gave blank screen Is... (1 Reply)
Discussion started by: sigh2010
1 Replies

9. Shell Programming and Scripting

awk column compare

I've been banging my head against the wall to accomplish the following. Given two files: File a.txt 12345 hello 32324 there File b.txt 12345 stuff 45454 howdy 32324 joe If column 1 matches between the two files, then print only the entire line of the 2nd file (b.txt in this... (3 Replies)
Discussion started by: tiggyboo
3 Replies

10. Shell Programming and Scripting

awk compare column between 2 files

Hi, I would like to compare file1 and file2 file1 1 2 3 file2 1 a 2 b 3 c 4 d The result should only print out "d" in file 2. Thanks (3 Replies)
Discussion started by: phamp008
3 Replies
Login or Register to Ask a Question