Exact match question


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Exact match question
# 8  
Old 10-09-2014
Quote:
Originally Posted by RavinderSingh13
Hello yuejian,

Not sure if this fulfills your requirement could you please try following and let me know.

Code:
awk -F" |" 'FNR==NR{A[$1]=$1;next} ($2 in A){B=B?B ORS A[$2] OFS $4:A[$2] OFS $4} END{print B}' file2  file1 | sort -t'(' -k2,2n
OR
awk -F" |" 'FNR==NR{A[$1]=$1;next} ($2 in A){print A[$2] OFS $4}' file2 file1 | sort -t'(' -k2,2n

Thanks,
R. Singh
Hi Singh,

I tried your code. It only ranks the lines start with "GALGA" in file2. Actually all lines in file2 can have the corresponding mfe value in file1. Could you also take a look again? Thank you.

Yuejian

---------- Post updated at 02:41 PM ---------- Previous update was at 02:30 PM ----------

Quote:
Originally Posted by Akshay Hegde
You have asked for exact match not partial match I think, do you mean

NM_001159315 == NM_001159315.1 ?
I am sorry Akshay and Singh, I guess my expression lead you guys misunderstanding, my bad. This is what I meant: if any line in file2 partially match line in file1, list the mfe value in the parenthesis and rank it. So in most situations, the line in file2 has only one partially matched line in file1. However, some line in file2 has multiple partially matched lines in file1, I only need the exact match for mfe value under this multiple partial matching situation. Basically, I want all lines in file2 have the corresponding mfe value in file1 and rank them. Sorry to make you confused.

Yuejian

Last edited by yuejian; 10-09-2014 at 03:46 PM..
# 9  
Old 10-09-2014
Quote:
Originally Posted by yuejian
Hi Singh,

I tried your code. It only ranks the lines start with "GALGA" in file2. Actually all lines in file2 can have the corresponding mfe value in file1. Could you also take a look again? Thank you.

Yuejian

---------- Post updated at 02:41 PM ---------- Previous update was at 02:30 PM ----------



I am sorry Akshay and Singh, I guess my expression lead you guys misunderstanding, my bad. This is what I meant: if any line in file2 partially match line in file1, list the mfe value in the parenthesis and rank it. So in most situations, the line in file2 has only one partially matched line in file1. However, some line in file2 has multiple partially matched lines in file1, I only need the exact match for mfe value under this multiple partial matching situation. Sorry to make you confused.

Yuejian
Okay try this

Code:
awk '
FNR == NR {
        key[$1]
        next
}
{      
	 $1 = $1
 
	 for(k in key)
         {
		if(match($0,k))
		{
			found = 0
			for(p=1; p<=NF; p++)
			{
				s = $p ; gsub(/\..*/,x,s)
				if(!found &&  s == k)
				{
					found = 1
				}
				if(found && $p ~ /mfe:/)
				{
					key[k]+=$(p+1)
					next
				}					
			}

		}
	 }       
}
END{
	for(k in key)print k,key[k]
}
'  FS='[ |]' f2 f1


Last edited by Akshay Hegde; 10-09-2014 at 04:13 PM.. Reason: Bug fix - need sleep
# 10  
Old 10-09-2014
Quote:
Originally Posted by Akshay Hegde
Okay try this I think in previous code I forgot to put FS

Try this then, if result is fine modify print statement according to your need.

Code:
awk '
FNR == NR {
        key[$1]
        next
}
{      
	 $1 = $1 
 
	 for(k in key)
         {
		if(match($0,k))
		{
			s = substr($0,RSTART,RLENGTH)
			found = 0
			for(p=1; p<=NF; p++)
			{
				if(!found && $p ~ s)
				{
					found = 1
				}
				if(found && $p ~ /mfe:/)
				{
					key[k]+=$(p+1)
					next
				}					
			}

		}
	 }       
}
END{
	for(k in key)print k,key[k]
}
'  FS='[ |]' f2 f1

Akshay, I tried this one. It didn't rank based on the mfe value and some lines missing mfe value. Also, in the output file, the "GALGA25_FK1" value is "-158.8" which is the sum of the partially match value.
# 11  
Old 10-09-2014
I edited my post please try
# 12  
Old 10-12-2014
I have looked at this thread (as you requested in PM) and I can't figure out what it is that you're trying to do. It appears that other people reading the thread are having the same problem.

You say you want an exact match, but when code is given to you that performs exact matches, you sometimes want partial matches. And if there are several partial matches we are supposed to magically guess which partial matches are supposed to be added in and which are to be ignored.

Until you can provide a clear explanation of what the code you want is supposed to do, there is little chance that you can write code that does what you want.
 
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 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

2. Shell Programming and Scripting

Help match the exact string

I just want to match "binutils1_test" only, and print the match line only lyang001@lyang001-OptiPlex-9010:/tmp$ cat file zbinutils1_test bbinutils1_test binutils1_test w-binutils1_test lyang001@lyang001-OptiPlex-9010:/tmp$ cat file |grep -w 'binutils1_test' ... (7 Replies)
Discussion started by: yanglei_fage
7 Replies

3. Shell Programming and Scripting

Grep exact match

Hello! I have 2 files named tacs.tmp and tacDB.txt tacs.tmp looks like this 0 10235647 102700 106800 107200 1105700 tacDB.txt looks like this 100100,Mitsubishi,G410,Handheld,,0,0,0 100200,Siemens,A53,Handheld,,0,0,0 100300,Sony Ericsson,TBD (AAB-1880030-BV),Handheld,,0,0,0... (2 Replies)
Discussion started by: Cludgie
2 Replies

4. UNIX for Dummies Questions & Answers

Interpolation if there is no exact match for value

Dear all, could you help me with following question. There are two datasets (below). I need to find match between BP values from data1 and data2, and add corresponding CM value from data2 into data1. if there is not exact match, the corresponding CM value should be calculated using interpolation.... (20 Replies)
Discussion started by: kush
20 Replies

5. Shell Programming and Scripting

Match exact and append zero

file 11 2 12 6 13 7 114 6 011 7 if I'm searching for 11, output needed is output: 11 2 011 7 Code: awk '$1 ~ /^11$/' file I used the above to match exact, but it avoiding "011 7" line too, how to resolve this? (6 Replies)
Discussion started by: Roozo
6 Replies

6. Shell Programming and Scripting

Exact match and #

Hi friends, i am using the following grep command for exact word match: >echo "sachin#tendulkar" | grep -iw "sachin" output: sachin#tendulkar as we can see in the above example that its throwinng the exact match(which is not the case as the keyword is sachin and string is... (6 Replies)
Discussion started by: neelmani
6 Replies

7. Shell Programming and Scripting

Exact match question

Hi, I have a file like follows . . . White.Jack.is.going.home Black.Jack.is.going.home Red.Jack.is.going.home Jack.is.going.home . . . when I make: cat <file> | grep -w "Jack.is.going.home" it gives: White.Jack.is.going.home Black.Jack.is.going.home Red.Jack.is.going.home... (4 Replies)
Discussion started by: salih81
4 Replies

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

9. Shell Programming and Scripting

Exact Word Match

I'm trying to find a exact word match but couldn't do it. ABC ABC_NE Searching for ABC_NE tried grep -w </ABC_NE/> grep "^ABC_NE$" but didn't worked , any awk variants would also help. ---------- Post updated at 08:40 AM ---------- Previous update was at 06:48 AM ---------- I... (2 Replies)
Discussion started by: dinjo_jo
2 Replies

10. Shell Programming and Scripting

How to get exact match sentences?

Hi, I have sentences like this: $sent= Protein modeling studies reveal that the RG-rich region is part of a three to four strand antiparallel beta-sheet, which in other RNA binding protein functions as a platform for nucleic acid interactions. Heterogeneous nuclear ribonucleoparticle... (19 Replies)
Discussion started by: vanitham
19 Replies
Login or Register to Ask a Question