Need help in comparing multiple columns from two files.


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Need help in comparing multiple columns from two files.
# 8  
Old 10-24-2014
Hi RudiC, The code which you gave is worked for my case. Thank U.
however i need to impelement the if as i mentioend in earlier post.

Code:
 if ( ( $2 ~ x[1] ) and ( $5 ~ x[2] ) )  or ( ( $2 ~ x[2] ) and ( $5 ~ x[1] ) ) { print $0 > "yes.txt"; } else { print $0 >

Please help on this
# 9  
Old 10-24-2014
Above doesn't work. Neither awk nor shell have "and" or "or". Replace by "&&" and "||" and try again. Why do you "need to impelement the if" ? Was your specification not describing the whole picture?
# 10  
Old 10-24-2014
Quote:
Originally Posted by sivarajb
Hi RudiC, The code which you gave is worked for my case. Thank U.
however i need to impelement the if as i mentioend in earlier post.

Code:
 if ( ( $2 ~ x[1] ) and ( $5 ~ x[2] ) )  or ( ( $2 ~ x[2] ) and ( $5 ~ x[1] ) ) { print $0 > "yes.txt"; } else { print $0 >

Please help on this
RudiC gave you code that you say works. Now you want us to redo that working code to use code that is incomplete and contains many syntax errors.

WHY do you NEED to change what RudiC suggested?

Is this a homework assignment?

You implied earlier that you had code using nested loops that gives you the correct results but is too slow to work on your actual data. Please show us your working code!

And, please clarify what you mean by match. Do you mean the two strings being matched are equal? Or, do you mean that a string in file1 matches a string in file2 if the string in file1 is contained in the string in file2?
# 11  
Old 10-28-2014
Thank you all for your valuable suggestions.. Some how the issue got resolved except the below one..

Now I need to print first column from file 2 and columns 1,2,4 from file 1 when ever I grt a match in below code...

Code:
awk     'NR==FNR                {a[$1,$2]; next}
         ($2 SUBSEP $5 in a)    {print a[$1], $1, $2,$4 > "yes.txt"; next}
                                {print $0 > "no.txt" }
        ' file2 file1

I am not able to get the result as expected. Please help
# 12  
Old 10-29-2014
As per your specification, $1 in file2 equals $2 in file1. So you could print $2, $1, $2, $4 in your script.
# 13  
Old 10-29-2014
Yes rudic..
You are correct.. I want to print first column from file 2 and some columns from file 1 when it matches...please help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing multiple columns using awk

Hello All; I have two files with below conditions: 1. Entries in file A is missing in file B (primary is field 1) 2. Entries in file B is missing in file A (primary is field 1) 3. Field 1 is present in both files but Field 2 is different. Example Content: File A ... (4 Replies)
Discussion started by: mystition
4 Replies

2. Shell Programming and Scripting

awk arrays comparing multiple columns across two files.

Hi, I'm trying to use awk arrays to compare values across two files based on multiple columns. I've attempted to load file 2 into an array and compare with values in file 1, but success has been absent. If anyone has any suggestions (and I'm not even sure if my script so far is on the right lines)... (4 Replies)
Discussion started by: hubleo
4 Replies

3. Shell Programming and Scripting

Comparing two columns from two different files

Hi, I have a single-column file1 having records like: 00AB01/11 43TG22/00 78RC09/34 ...... ...... and a second file , file 2 having two columns like 78RC09/34 1 45FD11/11 2 00AB01/11 3 43TG22/00 4 ...... ...... (8 Replies)
Discussion started by: amarn
8 Replies

4. Shell Programming and Scripting

comparing two columns from two different files

Hello, I have two files as 1.txt and 2.txt with number as columns. 1.txt 0 53.7988 1 -30.0859 2 20.1632 3 14.2135 4 14.6366 5 -37.6258 . . . 31608 -8.57333 31609 -2.58554 31610 -24.2857 2.txt (1 Reply)
Discussion started by: AKD
1 Replies

5. UNIX for Dummies Questions & Answers

Comparing columns in two files

Hi, I have two files. File1.txt has 2 columns and looks like: 458739 122345 4456 122657 34200 122600 File2.txt has many columns with column 1 the same as column2 of File1.txt, but with lot more rows: 122786 abcdefg user1@email 122778 uuhjeufh user2@email... (1 Reply)
Discussion started by: ursaan
1 Replies

6. Shell Programming and Scripting

comparing the values of repeated keys in multiple columns

Hi Guyz The 1st column of the input file has repeated keys like x,y and z. The ist task is if the 1st column has unique key (say x) and then need to consider 4th column, if it is + symbol then subtract 2nd column value with 3rd column value (we will get 2(10-8)) or if it is - symbol subtract 3rd... (3 Replies)
Discussion started by: repinementer
3 Replies

7. UNIX for Dummies Questions & Answers

Comparing 2 columns from 2 files

Hi, I have two files with the same number of columns. Basically I want to print the 2 columns that match between the two files. File1 looks like this: dr12 12 6 abn dr14 12 7 abn File2 looks something like this: dr12 12 8 abn dr12 14 7 abn So basically if the first... (1 Reply)
Discussion started by: kylle345
1 Replies

8. Shell Programming and Scripting

comparing 2 columns from 2 files

Hey, I have 2 files that have a name and then a number: File 1: dog 21 dog 24 cat 33 cat 27 dog 76 cat 65 File 2: dog 109 dog 248 cat 323 cat 207 cat 66 (2 Replies)
Discussion started by: dcfargo
2 Replies

9. Shell Programming and Scripting

Comparing Columns of two FIles

Dear all, I have two files in UNIX File1 and File2 as below: File1: 1,1234,.,67.897,,0 1,4134,.,87.97,,4 0,1564,.,97.8,,1 File2: 2,8798,.,67.897,,0 2,8879,.,77.97,,4 0,1564,.,97.8,,1 I want to do the following: (1) Make sure that both the files have equal number of columns and if... (4 Replies)
Discussion started by: ggopal
4 Replies

10. UNIX for Advanced & Expert Users

Comparing Columns of two FIles

Dear all, I have two files in UNIX File1 and File2 as below: File1: 1,1234,.,67.897,,0 1,4134,.,87.97,,4 0,1564,.,97.8,,1 File2: 2,8798,.,67.897,,0 2,8879,.,77.97,,4 0,1564,.,97.8,,1 I want to do the following: (1) Make sure that both the files have equal number of columns and if... (1 Reply)
Discussion started by: ggopal
1 Replies
Login or Register to Ask a Question