Match or no match


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Match or no match
# 1  
Old 12-19-2014
Match or no match

Not sure if my attempt below is close but I am trying to match $1 of gene.txt to $2 of medicalexome.txt,
and if there is a match then $2 $1 are copied to a new result.txt which is tab-delimited. If no match is found then
$1 of gene.txt and "No Match" are copied to the result file.


Code:
 awk 'FNR==NR { E[$1]=$2 ; next }
{ $1=$1 in E?E[$1]:"No Match" } 1' medicalexome.txt gene.txt > result.txt OFS="/t"

So, the first name in gene.txt is A2m and that does not match anything in medicalexome.txt so A2M No Match is copied to result.txt

the 213 name in gene.txt is ALK and that matches medicalexome.txt so ALK 105590 is copied to result.txt

Thank you Smilie.
Moderator's Comments:
Mod Comment CODE tags are needed around sample input, output, and code; not around descriptive text explaining what the tagged segments contain.

Last edited by Don Cragun; 12-19-2014 at 10:09 PM.. Reason: Remove extraneous CODE tags.
# 2  
Old 12-19-2014
Try something like this:

Code:
awk 'FNR==NR {a[$2]=$1;next}; {b=$1; if (b in a) print $1 "\t" a[$1]; else print $1 "\t" "No Match"};' medicalexome.txt gene.txt > result.txt

# 3  
Old 12-20-2014
@OP: Modifications to your code:
Code:
awk 'FNR==NR { E[$2]=$1 ; next } { $2=$1 in E?E[$1]:"No Match" } 1' OFS="\t" medicalexome.txt gene.txt > result.txt

# 4  
Old 12-20-2014
Hi Scrutinizer,
Can you please educate me re: the below items in red? Why is $1 being set to $2 and what is E? doing?

Code:
awk 'FNR==NR { E[$2]=$1 ; next } { $2=$1 in E?E[$1]:"No Match" } 1' OFS="\t" medicalexome.txt gene.txt > result.txt

# 5  
Old 12-20-2014
Quote:
Originally Posted by mjf
Hi Scrutinizer,
Can you please educate me re: the below items in red? Why is $1 being set to $2 and what is E? doing?

Code:
awk 'FNR==NR { E[$2]=$1 ; next } { $2=$1 in E?E[$1]:"No Match" } 1' OFS="\t" medicalexome.txt gene.txt > result.txt

Hi mjf,
$2 is not being set to $1.
If $1 is present as an index in E[] ($1 in E), $2 is being set to E[$1]; otherwise $2 is being set to the string No Match.
These 2 Users Gave Thanks to Don Cragun For This Post:
# 6  
Old 12-20-2014
Got it! Thanks Don. I was unfamilar with the conditional C operator.
# 7  
Old 12-20-2014
Quote:
Originally Posted by mjf
Got it! Thanks Don. I was unfamilar with the conditional C operator.
You're welcome. condition ? TrueResult : FalseResult is a very powerful shorthand in both C and awk. Isn't this a great place to learn about the tools we all use?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Data match 2 files based on first 2 columns matching only and join if match

Hi, i have 2 files , the data i need to match is in masterfile and i need to pull out column 3 from master if column 1 and 2 match and output entire row to new file I have tried with join and awk and i keep getting blank outputs or same file is there an easier way than what i am... (4 Replies)
Discussion started by: axis88
4 Replies

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

3. Shell Programming and Scripting

awk to update file based on partial match in field1 and exact match in field2

I am trying to create a cronjob that will run on startup that will look at a list.txt file to see if there is a later version of a database using database.txt as the source. The matching lines are written to output. $1 in database.txt will be in list.txt as a partial match. $2 of database.txt... (2 Replies)
Discussion started by: cmccabe
2 Replies

4. Shell Programming and Scripting

awk to match field between two files and use conditions on match

I am trying to look for $2 of file1 (skipping the header) in $2 of file2 (skipping the header) and if they match and the value in $10 is > 30 and $11 is > 49, then print the line from file1 to a output file. If no match is foung the line is not printed. Both the input and output are tab-delimited.... (3 Replies)
Discussion started by: cmccabe
3 Replies

5. Shell Programming and Scripting

Search from 1st match and end 2nd match

I've been looking through the forums for awhile now and looking at the man page for grep and egrep and not seeming to find this scenario so it might not be possible but figured I'd throw it out to get some ideas. I'm looking for a way to search a file for 1st match (example below net self) and... (3 Replies)
Discussion started by: djzah
3 Replies

6. Shell Programming and Scripting

Display match or no match and write a text file to a directory

The below bash connects to a site, downloads a file, searches that file based of user input - could be multiple (all that seems to work). What I am not able to figure out is how to display on the screen match found or no match found" and write a file to a directory (C:\Users\cmccabe\Desktop\wget)... (4 Replies)
Discussion started by: cmccabe
4 Replies

7. Shell Programming and Scripting

Match pattern1 in file, match pattern2, substitute value1 in line

not getting anywhere with this an xml file contains multiple clients set up with same tags, different values. I need to parse the file for client foo, and change the value of tag "64bit" from false to true. cat clients.xml <Client type"FIX"> <ClientName>foo</ClientName>... (3 Replies)
Discussion started by: jack.bauer
3 Replies

8. UNIX for Dummies Questions & Answers

awk display the match and 2 lines after the match is found.

Hello, can someone help me how to find a word and 2 lines after it and then send the output to another file. For example, here is myfile1.txt. I want to search for "Error" and 2 lines below it and send it to myfile2.txt I tried with grep -A but it's not supported on my system. I tried with awk,... (4 Replies)
Discussion started by: eurouno
4 Replies

9. Shell Programming and Scripting

exact string match ; search and print match

I am trying to match a pattern exactly in a shell script. I have tried two methods awk '/\<mpath${CURR_MP}\>/{print $1 $2}' multipath perl -ne '/\bmpath${CURR_MP}\b/ and print' /var/tmp/multipath Both these methods require that I use the escape character. I am guessing that is why... (8 Replies)
Discussion started by: bash_in_my_head
8 Replies

10. Shell Programming and Scripting

Need help to grep for a title match and then make some queries after the match

Here is the sample of my file address.txt Address 1 1234 Drive way New Orleans, LA Zipcode :- 12345 Address 2 4567 Spring way Chicago, IL Zipcode :- 67890 I would like to grep for an Address title (Ex :- Address 2) , then get its zipcode and echo both in a single line. Ex :- ... (3 Replies)
Discussion started by: leo.maveriick
3 Replies
Login or Register to Ask a Question