Continued trouble matching fields in different files and selective field printing ([g]awk)
I apologize in advance, but I continue to have trouble searching for matches between two files and then printing portions of each to output in [g]awk and would very much appreciate some help.
I have data as follows:
File1
File2
If $2 and $5 of File2 match $1 and $2 of File1, I would like to print $4 of File2 followed by $0 of File1.
Intended Output:
I have been attempting the following code to no avail.
Close. Note that in the command:
the NR==FNR condition section is gathering data from the first file where you are looking at fields $1 and $2. So that line looks OK, but since you aren't modifying either of those fields, you don't need the q variable to keep those values (like you did in your last thread) so that line can be written more simply as:
In the other line of code, you're looking at fields $2 and $5 from the 2nd input file. You got that right in one case, but you didn't in the other case. If you change:
to:
you should get what you want.
This User Gave Thanks to Don Cragun For This Post:
Thank you so much for this Don and also for your (and others') patience as I posted two very similar threads that kept tripping me up (I continue to have trouble with this sort of text manipulation).
I may be wrong, but in the code you suggest,
it looks like that will print all of File2 (i.e., the entire line) prior to the matched output.
Thank you so much for this Don and also for your (and others') patience as I posted two very similar threads that kept tripping me up (I continue to have trouble with this sort of text manipulation).
I may be wrong, but in the code you suggest,
it looks like that will print all of File2 (i.e., the entire line) prior to the matched output.
Perhaps
does the trick.
Sorry about that. I didn't look closely enough at your output spec after noticing the wrong fields being used as indexes into your A[] array. If we go back to your 1st post in this thread, you said:
Quote:
If $2 and $5 of File2 match $1 and $2 of File1, I would like to print $4 of File2 followed by $0 of File1.
so instead of:
it would seem that you might need:
And, of course, you still need the condition on that action:
This User Gave Thanks to Don Cragun For This Post:
Please don't be sorry. I appreciate your help more than you know.
---------- Post updated at 08:38 PM ---------- Previous update was at 07:00 PM ----------
Don, if I may, your code help works terrific except I'm having issues when the records of $1 and $2 are repeated somewhere else in the field.
For example, there is a section in my data that looks like this portion of File1:
File1
Here, there are two instances where $1=="PS12,007" and $2=="DKJ" (the first and last lines in the example, but these are not first and last in the actual file). However there is different data in fields $3-$23.
When I run the code you helped me with:
with the following portion of File2:
I get this output:
Here, fields $4-$24 are identical when fields $1 and $2 of File1 have a repeated string. However, fields $4-$24 should remain as they appear in File1 (i.e., fields $3-$23 in File1). The end result should look like this:
I've attempted various code variations that take what you had helped me with and integrates it with count and/or loop functions in awk. None of these have been successful.
If you are using only $1 & $2 as a key into file1 to match against $2 & $5 as a key into file2 and there those keys do not uniquely identify which record is to be processed when the data associated with those keys are not unique, there isn't anything I can do to help you. When there are multiple records with the same keys, you need to clearly describe how your code is supposed to determine which of those records are supposed to be paired.
The way the code is written now, the last record with a given key in file1 will be matched with all records with that key in file2. We now know that that isn't what you want. How do you uniquely describe a key that can be used to get what you do want? Or, in other words, what do you want to add to your current keys so your keys will uniquely identify the records you want to match in both files?
This User Gave Thanks to Don Cragun For This Post:
The fact that combinations of $2 and $5 can occur multiple times is a new condition that was not apparent in the first post. So what is the criterion for what goes with what? Is it the order in which they appear in both files or does $3 in file2 play a role somehow?
Assuming it is the former of these two possibillties, try this modification:
which (with the new sample in post #5) produces:
==edit:==
The above code can create problems if it is possible combinations only appear once in file1 and twice in file2, which would lead to half lines being printed.
In that case it could be modified to this:
So that would mean that a second occurrence in file2 would not get printed if it only appears once in file 1
Last edited by Scrutinizer; 01-06-2018 at 12:59 PM..
This User Gave Thanks to Scrutinizer For This Post:
Hello all, I am having trouble with what should be an easy task, but seem to be missing something fundamental. I have two files, with File 1 consisting of a single field of many thousands of records. I also have File 2 with two fields and many thousands of records.
My goal is that when $1 of... (2 Replies)
Hi Gurus,
I have following input file. I tried multiple awk combinations to print selected columns without success.
HEX ID Name ver FLRGT Start Time Total Shared End Date
----- -------- --- ------ ------------------------ -------------- -------... (4 Replies)
Hi,
I have 2 tab-delimited input files as follows.
file1.tab:
green A apple
red B apple
file2.tab:
apple - A;Z
Objective:
Return $1 of file1 if,
. $1 of file2 matches $3 of file1 and,
. any single element (separated by ";") in $3 of file2 is present in $2 of file1
In order to... (3 Replies)
In the below I am trying to use awk to match all the $13 values in input, which is tab-delimited,
that are in $1 of gene which is just a single column of text.
However only the line with the greatest $9 value in input needs to be printed.
So in the example below all the MECP2 and LTBP1... (0 Replies)
Dear all,
I have been trying to print an entire field, if the first line of the field is matching.
For example, my input looks something like this.
aaa ddd zzz
123 987 126
24 0.650 985
354 9864 0.32
0.333 4324 000
I am looking for a pattern,... (5 Replies)
Hi,
i need help to print number from different field
INPUT:
Student1 10 20
Student2 30 40
Student3 50 60
Student4 70 80
Desired Output:
1 20-30
2 40-50
3 60-70
Thank you! (5 Replies)
Dear All,
I would like to add values of a field, if the lines match in a certain field. Then I would like to divide the sum though the number of lines that have a matched field. This is the Input:
Input:
Test1 5
Test1 10
Test2 2
Test2 5
Test2 13
Test3 4
Output:
Test1 7.5
Test1 7.5... (6 Replies)
Hello!
I am writing a program to run through two large lists of data (~300,000 rows), find where rows in one file match another, and combine them based on matching fields. Due to the large file sizes, I'm guessing AWK will be the most efficient way to do this. Overall, the input and output I'm... (5 Replies)
Hi there
my file looks like this
1 a b c d e f
2 a b b c d e f f g h e t t
3 a c b d e f
4 a b c
i want to print the line which has the fields containing ONLY a b c, in this case the line 4.
How can i awk it !!!?
Many Thanks in advance! (8 Replies)
I'm hoping someone can help me on this. I have a data file that greatly simplified might look like this:
sec;src;dst;proto
421;10.10.10.1;10.10.10.2;tcp
426;10.10.10.3;10.10.10.4;udp
442;10.10.10.5;10.10.10.6;tcp
sec;src;fac;dst;proto
521;10.10.10.1;ab;10.10.10.2;tcp... (3 Replies)