Match single line in file1 to groups of lines in file2


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match single line in file1 to groups of lines in file2
# 1  
Old 03-05-2014
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.
Code:
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.
Code:
>comp11084_c0_seq6:130-468(-)
MRYVAAYLLASLSGKEPSSDEVEKILSSVGIESDSSKLSLVIKELKGKNVDEVIESGRSKLAS
>comp12746_c0_seq3:140-478(+)
MRYVAAYLLASLSGKEPSSDEVEKILSSVGIESDSSKLSLVIKELKGKNVDEVIESGRSKLAS

>comp11084_c0_seq3:201-539(-)
MRYVAAYLLASFSGKEPTSDEIEKILSSVGIESDSDKVSLVVKELKGKNVDEVIESGRSKLAS
>comp12746_c0_seq2:191-529(+)
MRYVAAYLLASFSGKEPTSDEIEKILSSVGIESDSDKVSLVVKELKGKNVDEVIESGRSKLAS

>comp11084_c0_seq3:201-539(-)
MSDTSNVNRLEELGKMKVNDLKKELKARGLSTVGNKQELIDRMINHSESSVLDIEDTVLDE
>comp12601_c0_seq4:132-965(-)
MSDTSNVNRLEELGKMKVNDLKKELKARGLSTVGNKQELIDRMINHSESSVLDIEDTVLDE

All pairs of terms in file 1 "head" a pair of sequences in file 2. These are the pairs of sequences I want to extract. File 2 also has sequence pairs with headers not found in as pairs in file 1 (e.g. the third sequence in this example), which I want to exclude.

Output:
Code:
>comp11084_c0_seq6:130-468(-)
MRYVAAYLLASLSGKEPSSDEVEKILSSVGIESDSSKLSLVIKELKGKNVDEVIESGRSKLAS
>comp12746_c0_seq3:140-478(+)
MRYVAAYLLASLSGKEPSSDEVEKILSSVGIESDSSKLSLVIKELKGKNVDEVIESGRSKLAS

>comp11084_c0_seq3:201-539(-)
MRYVAAYLLASFSGKEPTSDEIEKILSSVGIESDSDKVSLVVKELKGKNVDEVIESGRSKLAS
>comp12746_c0_seq2:191-529(+)
MRYVAAYLLASFSGKEPTSDEIEKILSSVGIESDSDKVSLVVKELKGKNVDEVIESGRSKLAS

I can print lines that match in two files
Code:
awk ' NR == FNR { arr[$1$2]=1; next } arr[$2$1] {print $1, $2} '

But I don't know how to deal with matching one line in file 1 to multiple lines in file2.
Help me out?
Moderator's Comments:
Mod Comment Please use CODE tags when displaying code, input, and output; not QUOTE tags.

Last edited by Don Cragun; 03-05-2014 at 11:51 PM.. Reason: Replace QUOTE tags with CODE tags.
# 2  
Old 03-06-2014
You could try something like this:
Code:
awk 'NR==FNR{A[">" $1,">" $2]; next} ($1,$3) in A' file1 RS= ORS='\n\n file2

or
Code:
awk 'NR==FNR{A[$1,$2]; next} ($2,$5) in A' file1 FS=\> RS= ORS='\n\n' file2

They make use of the empty lines between records in the second file, by using an empty RS variable..

Last edited by Scrutinizer; 03-06-2014 at 02:12 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

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

2. UNIX for Dummies Questions & Answers

Compare file1 and file2, print matching lines in same order as file1

I want to print only the lines in file2 that match file1, in the same order as they appear in file 1 file1 file2 desired output: I'm getting the lines to match awk 'FNR==NR {a++}; FNR!=NR && a' file1 file2 but they are in sorted order, which is not what I want: Can anyone... (4 Replies)
Discussion started by: pathunkathunk
4 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

Compare file1 for matching line in file2 and print the difference in matching lines

Hello, I have two files file 1 and file 2 each having result of a query on certain database tables and need to compare for Col1 in file1 with Col3 in file2, compare Col2 with Col4 and output the value of Col1 from File1 which is a) not present in Col3 of File2 b) value of Col2 is different from... (2 Replies)
Discussion started by: RasB15
2 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

Match one column of file1 with that of file2

Hi, I have file1 like this aaa ggg ddd vvv eeeand file2 aaa 2 aaa 443 xxx 76 aaa 34 ggg 33 wee 99 ggg 33 ddd 1 ddd 10 ddd 98 sds 23 (4 Replies)
Discussion started by: polsum
4 Replies

9. Shell Programming and Scripting

Display lines from file1 that are not in file2

Hi there, I know the command diff but what I want is slightly different. I have two files containing lines that look like md5sums. file1 5a1e8cee2eb2157c86e7266ee38e47c3 /tmp/file1 a254c48bdd064a40b82477b9fa5be05d /tmp/file2 2d57c72ec898acddf8a6bacb3f821572 /tmp/file3... (5 Replies)
Discussion started by: chebarbudo
5 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