Match one column of file1 with that of file2


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match one column of file1 with that of file2
# 1  
Old 03-02-2012
Match one column of file1 with that of file2

Hi,

I have file1 like this
PHP Code:
aaa  
ggg
ddd
vvv
eee 
and file2
PHP Code:
aaa  2
aaa  443
xxx 76
aaa 34
ggg 33
wee 99
ggg 33
ddd 1
ddd 10
ddd 98
sds 23
vvv 32
eee 11
eee 87 
Match the column of file1 with first column of file2 and print out lines in file2 with corresponding highest value in column2 of file2.
desired output
PHP Code:
aaa 443
ggg 33
ddd 98
vvv 32
eee 87 
thanks in advance.
# 2  
Old 03-02-2012
Try this:
Code:
awk 'NR==FNR{a[$1]=1;next}($1 in a) && $2 > a[$1]{a[$1]=$2}END{for(i in a)print i, a[i]}' file1 file2

This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 03-02-2012
Code:
awk 'NR==FNR{a[$1]=1;next}($1 in a) && $2 > a[$1]{a[$1]=$2}END{for(i in a)print i, a[i]}' file1 file2

Hi Franklin52, thanks a lot for the reply. Its working just fine.

However, I also have another set of files which are similar to the above mentioned but slightly different orientation. And I couldnt apply your code to them. Here are the files.

file1
PHP Code:
aaa  
ggg
ddd
vvv
eee 
and file2
PHP Code:
ACC 2 2 21 aaa 
AC 443 3 22 aaa  
GCT 76 1 33 xxx 
TCG 34 2 33 aaa 
ACGT 33 1 22  ggg 
TTC 99 3 44 wee 
CCA 33 2 33 ggg 
AAC 1 3 55 ddd 
TTG 10 1 22 ddd 
TTGC 98 3 22 ddd 
GCT 23 1 21 sds 
ACGT 32 2 33 vvv 
CGT 11 2 33 eee 
CCC 87 2 44 eee 
As you can see there are 5 columns now in file2. Match the column of file1 with fifth column of file2 and print out the complete lines in file2 with corresponding highest value in column2 of file2. If the values are same and no highest value, just print the first line of matching.
desired output
PHP Code:
AC 443 3 22 aaa  
ACGT 33 1 22  ggg
TTGC 98 3 22 ddd 
ACGT 32 2 33 vvv 
CCC 87 2 44 eee 
thanks in advance.

thanks again
# 4  
Old 03-02-2012
Code:
awk 'NR==FNR{a[$1]=1;next} a[$5]{if($2 > a[$5]){a[$5]=$2;b[$5]=$0}} END{for(i in b)print b[i]}' file1 file2

This User Gave Thanks to Franklin52 For This Post:
# 5  
Old 03-03-2012
Quote:
Originally Posted by Franklin52
Code:
awk 'NR==FNR{a[$1]=1;next} a[$5]{if($2 > a[$5]){a[$5]=$2;b[$5]=$0}} END{for(i in b)print b[i]}' file1 file2

thanks a lot SmilieSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Keep only the closet match of timestamped row (include headers) from file1 to precede file2 row/s

This is a question that is related to one I had last August when I was trying to sort/merge two files by millsecond time column (in this case column 6). The script (below) that helped me last august by RudiC solved the puzzle of sorting/merging two files by time, except it gets lost when the... (0 Replies)
Discussion started by: aachave1
0 Replies

2. Shell Programming and Scripting

awk to search field2 in file2 using range of fields file1 and using match to another field in file1

I am trying to use awk to find all the $2 values in file2 which is ~30MB and tab-delimited, that are between $2 and $3 in file1 which is ~2GB and tab-delimited. I have just found out that I need to use $1 and $2 and $3 from file1 and $1 and $2of file2 must match $1 of file1 and be in the range... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. Shell Programming and Scripting

Print sequences from file2 based on match to, AND in same order as, file1

I have a list of IDs in file1 and a list of sequences in file2. I can print sequences from file2, but I'm asking for help in printing the sequences in the same order as the IDs appear in file1. file1: EN_comp12952_c0_seq3:367-1668 ES_comp17168_c1_seq6:1-864 EN_comp13395_c3_seq14:231-1088... (5 Replies)
Discussion started by: pathunkathunk
5 Replies

4. Shell Programming and Scripting

Match single line in file1 to groups of lines in file2

I have two files. File 1 is a two-column index file, e.g. comp11084_c0_seq6:130-468(-) comp12746_c0_seq3:140-478(+) comp11084_c0_seq3:201-539(-) comp12746_c0_seq2:191-529(+) File 2 is a sequence file with headers named with the same terms that populate file 1. ... (1 Reply)
Discussion started by: pathunkathunk
1 Replies

5. Shell Programming and Scripting

Match part of string in file2 based on column in file1

I have a file containing texts and indexes. I need the text between (and including ) INDEX and number "1" alone in line. I have managed this: awk '/INDEX/,/1$/{if (!/1$/)print}' file1.txt It works for all indexes. And then I have second file with years and indexes per year, one per line... (3 Replies)
Discussion started by: phoebus
3 Replies

6. Shell Programming and Scripting

Based on column in file1, find match in file2 and print matching lines

file1: file2: I need to find matches for any lines in file1 that appear in file2. Desired output is '>' plus the file1 term, followed by the line after the match in file2 (so the title is a little misleading): This is honestly beyond what I can do without spending the whole night on it, so I'm... (2 Replies)
Discussion started by: pathunkathunk
2 Replies

7. UNIX for Dummies Questions & Answers

if matching strings in file1 and file2, add column from file1 to file2

I have very limited coding skills but I'm wondering if someone could help me with this. There are many threads about matching strings in two files, but I have no idea how to add a column from one file to another based on a matching string. I'm looking to match column1 in file1 to the number... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

8. 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

9. Shell Programming and Scripting

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2

Match column 3 in file1 to column 1 in file 2 and replace with column 2 from file2 file 1 sample SNDK 80004C101 AT XLNX 983919101 BB NETL 64118B100 BS AMD 007903107 CC KLAC 482480100 DC TER 880770102 KATS ATHR 04743P108 KATS... (7 Replies)
Discussion started by: rydz00
7 Replies

10. Shell Programming and Scripting

match value from file1 in file2

Hi, i've two files (file1, file2) i want to take value (in column1) and search in file2 if the they match print the value from file2. this is what i have so far. awk 'FILENAME=="file1"{ arr=$1 } FILENAME=="file2" {print $0} ' file1 file2 (2 Replies)
Discussion started by: myguess21
2 Replies
Login or Register to Ask a Question