Sponsored Content
Top Forums UNIX for Beginners Questions & Answers Match Fields between two files, print portions of each file together when matched in ([g]awk)' Post 303010668 by Scrutinizer on Friday 5th of January 2018 04:55:28 AM
Old 01-05-2018
Perhaps these modifications make it more what you are looking for:
Code:
FNR==NR {
         q=$1 SUBSEP $2
         $1=$2=""
         A[q]=$0
         next
}
{
if($2 !~/\?/ && ($1,$2) in A)
         print $0,A[$1,$2]
else if($2 ~/\?/)
         print $0, " - - - - - - - - - - - - - - - - - - - - - "
}

This is using both field 1 and 2 instead of only field 2.

It produces:
Code:
PS009,001 L          ?            0 5 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 5 0 -1 -1 -1 -1 -1 -1 -1
PS009,001 H          ?            0 0 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 0 0 -1 -1 -1 -1 -1 -1 -1
PS009,001 NYX        ?            0 1 7 1 1 1 -1 6 0 1 2 2 2 5 2 -1 -1 -1 0 521 0
PS009,001 <LMWT      ?            0 2 -1 -1 -1 1 -1 -1 -1 1 1 2 2 2 1 -1 -1 -1 0 521 0
PS009,001 L          ?            0 5 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 5 0 -1 -1 -1 -1 -1 -1 -1
PS009,001 H          ?            0 0 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 0 0 -1 -1 -1 -1 -1 -1 -1
PS009,001 BN         ?            0 2 -1 -1 -1 1 -1 -1 -1 1 2 2 2 5 2 -1 -1 -1 -11 582 0
PS009,001 MZMWR      ?            0 2 -1 -1 -1 1 -1 -1 -1 1 2 2 2 2 1 -1 -1 -1 0 521 0
PS009,001 L          ?            0 5 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 5 0 -1 -1 -1 -1 -1 -1 -1
PS009,001 DWD        ?            0 3 -1 -1 -1 1 -1 -1 -1 1 2 2 3 5 2 -1 -1 -1 -11 582 0
PS009,002 JDH        JDJ          0 1 4 2 1 -1 -1 1 1 1 0 -1 1 1 -1 -1 -1 -1 0 501 0
PS009,002 ?          L           - - - - - - - - - - - - - - - - - - - - - 
PS009,002 JHWH       MRJ>         0 3 -1 -1 -1 1 -1 -1 -1 1 2 2 3 3 2 -1 -1 -1 0 503 0
PS009,002 B          MN           0 5 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 5 0 -1 -1 -1 -1 -1 -1 -1
PS009,002 KL         KL           -6 2 -1 -1 -1 1 -1 -1 -1 1 2 1 2 0 -1 2 -1 -1 -1 -1 -1
PS009,002 ?          H           - - - - - - - - - - - - - - - - - - - - - 
PS009,002 LB         LB           0 2 -1 -1 -1 1 -1 -1 -1 1 2 1 2 0 -1 -1 -1 -1 -1 -1 -1
PS009,002 J          J            -1 7 -1 -1 -1 -1 -1 -1 1 1 -1 -1 7 5 2 -20102 -1 -1 0 505 0
PS009,002 ?          W           - - - - - - - - - - - - - - - - - - - - - 
PS009,002 SPR        C<J          0 1 4 1 18 -1 -1 1 1 1 0 -1 1 1 -1 -1 -1 -1 0 501 0
PS009,002 KL         KL           -6 2 -1 -1 -1 1 -1 -1 -1 1 2 1 2 0 -1 2 -1 -1 -1 -1 -1
PS009,002 ?          HJN         - - - - - - - - - - - - - - - - - - - - - 
PS009,002 PL>        TDMWR>       0 1 0 3 1 12 -1 6 0 3 1 1 2 0 -1 -1 -1 -1 -1 -1 -1
PS009,002 K          K            -1 7 -1 -1 -1 -1 -1 -1 2 1 2 -1 7 2 2 -20102 -1 -1 0 503 0
PS009,003 FMX        XDJ          0 1 4 0 18 -1 -1 1 1 1 0 -1 1 1 -1 -1 -1 -1 0 501 0
PS009,003 W          W            0 6 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 6 6 -1 -1 -1 -1 0 509 0
PS009,003 <LY        RWZ=         0 1 4 0 18 -1 -1 1 1 1 0 -1 1 1 -1 -1 -1 -1 0 501 0
PS009,003 B          B            0 5 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 5 0 -1 -1 -1 -1 -1 -1 -1
PS009,003 K          K            -1 7 -1 -1 -1 -1 -1 -1 2 1 2 -1 7 2 2 -1 -1 -1 0 503 0
PS009,003 ?          W           - - - - - - - - - - - - - - - - - - - - - 
PS009,003 ZMR        ZMR==        0 1 4 1 18 -1 -1 1 1 1 0 -1 1 1 -1 -1 -1 -1 0 501 0
PS009,003 ?          L           - - - - - - - - - - - - - - - - - - - - - 
PS009,003 CM         CM           0 2 -1 -1 -1 1 -1 -1 -1 1 2 1 2 0 -1 -1 -1 -1 -1 -1 -1
PS009,003 K          K   -1 7 -1 -1 -1 -1 -1 -1 2 1 2 -1 7 2 2 -1 -1 -1 0 503 0

This User Gave Thanks to Scrutinizer For This Post:
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk print fields to multiple files?

I am trying to print the output of a command to two separate files. Is it possible to use awk to print $1 to one file and $2 to another file? Thanks in advance! (1 Reply)
Discussion started by: TheCrunge
1 Replies

2. Shell Programming and Scripting

Match a line in File 1 with Column in File 2 and print whole line in file 2 when matched

Hi Experts, I am very new to scripting and have a prb since few days and it is urgent to solve so much appreciated if u help me. i have 2 files file1.txt 9647810043118 9647810043126 9647810043155 9647810043161 9647810043166 9647810043185 9647810043200 9647810043203 9647810043250... (22 Replies)
Discussion started by: mustafa.abdulsa
22 Replies

3. Shell Programming and Scripting

awk help: Match data fields from 2 files & output results from both into 1 file

I need to take 2 input files and create 1 output based on matches from each file. I am looking to match field #1 in both files (Userid) and create an output file that will be a combination of fields from both file1 and file2 if there are any differences in the fields 2,3,4,5,or 6. Below is an... (5 Replies)
Discussion started by: ambroze
5 Replies

4. Shell Programming and Scripting

Regex: print matched line and exact pattern match

Hi experts, I have a file with regexes which is used for automatic searches on several files (40+ GB). To do some postprocessing with the grep result I need the matching line as well as the match itself. I know that the latter could be achieved with grep's -o option. But I'm not aware of a... (2 Replies)
Discussion started by: stresing
2 Replies

5. Shell Programming and Scripting

awk to print fields that match using conditions and a default value for non-matching in two files

Trying to use awk to match the contents of each line in file1 with $5 in file2. Both files are tab-delimited and there may be a space or special character in the name being matched in file2, for example in file1 the name is BRCA1 but in file2 the name is BRCA 1 or in file1 name is BCR but in file2... (6 Replies)
Discussion started by: cmccabe
6 Replies

6. UNIX for Beginners Questions & Answers

Match Strings between two files, print portions of each file together when matched ([g]awk)

I have two files and desire to use the strings from $1 of file 1 (file1.txt) as search criteria to find matches in $2 of file 2 (file2.txt). If matches are found I want to output the entire line of file 2 (file2.txt) followed by fields $2-$11 of file 1 (file1.txt). I can find the matches, I cannot... (7 Replies)
Discussion started by: jvoot
7 Replies

7. Shell Programming and Scripting

awk to print match or non-match and select fields/patterns for non-matches

In the awk below I am trying to output those lines that Match between file1 and file2, those Missing in file1, and those missing in file2. Using each $1,$2,$4,$5 value as a key to match on, that is if those 4 fields are found in both files the match, but if those 4 fields are not found then missing... (0 Replies)
Discussion started by: cmccabe
0 Replies

8. Shell Programming and Scripting

awk move select fields to match file prefix in two directories

In the awk below I am trying to use the file1 as a match to file2. In file2 the contents of $5,&6,and $7 (always tab-delimited) and are copied to the output under the header Quality metrics. The below executes but the output is empty. I have added comments to help and show my thinking. Thank you... (0 Replies)
Discussion started by: cmccabe
0 Replies

9. Shell Programming and Scripting

awk to update file based on match in 3 fields

Trying to use awk to store the value of $5 in file1 in array x. That array x is then used to search $4 of file1 to find aa match (I use x to skip the header in file1). Since $4 can have multiple strings in it seperated by a , (comma), I split them and iterate througn each split looking for a match.... (2 Replies)
Discussion started by: cmccabe
2 Replies

10. UNIX for Beginners Questions & Answers

awk match two fields in two files

Hi, I have two TEST files t.xyz and a.xyz which have three columns each. a.xyz have more rows than t.xyz. I will like to output rows at which $1 and $2 of t.xyz match $1 and $2 of a.xyz. Total number of output rows should be equal to that of t.xyz. It works fine, but when I apply it to large... (6 Replies)
Discussion started by: geomarine
6 Replies
All times are GMT -4. The time now is 09:48 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy