sop_se
sop_se

Reputation: 21

bash grep variable as pattern

I don't usually work in bash but grep could be a really fast solution in this case. I have read a lot of questions on grep and variable assignment in bash yet I do not see the error. I have tried several flavours of double quotes around $pattern, used `...`` or $(...) but nothing worked.

So here's what I try to do: I have two files. The first contains several names. Each of them I want to use as a pattern for grep in order to search them in another file. Therefore I loop through the lines of the first file and assign the name to the variable pattern. This step works as the variable is printed out properly. But somehow grep does not recognize/interpret the variable. When I substitute "$pattern" with an actual name everything is fine as well. Therefore I don't think the variable assignment has a problem but the interpretation of "$pattern" as the string it should represent.

Any help is greatly appreciated!

#!/bin/bash

while IFS='' read -r line || [[ -n $line ]]; do
    a=( $line )
    pattern="${a[2]}"
    echo "Text read from file: $pattern"
    var=$(grep "$pattern" 9606.protein.aliases.v10.txt)
    echo "Matched Line in Alias is: $var"
done < "$1"


> bash match_Uniprot_StringDB.sh ~/Chromatin_Computation/.../KDM.protein.tb

output:

Text read from file: "UBE2B" 
Matched Line in Alias is: 
Text read from file: "UTY"
Matched Line in Alias is: 

EDIT The solution drvtiny suggested works. It is necessary to get rid of the double quotes to match the string. Adding the following lines makes the script work.

pattern="${pattern#\"}"
pattern="${pattern%\"}"

Upvotes: 1

Views: 1975

Answers (1)

drvtiny
drvtiny

Reputation: 715

Please, look at "-f FILE" option in man grep. I advise that this option do exactly what you need without any bash loops or such other "hacks" :)

And yes, according to the output of your code, you read pattern including double quotes literally. In other words, you read from file ~/Chromatin_Computation/.../KDM.protein.tb this string:

"UBE2B"

But not

UBE2B
  • as you probably expect.

Maybe you need to remove double quotes on the boundaries of your $pattern?

Try to do this after reading pattern:

pattern=${pattern#\"}
pattern=${pattern%\"}

Upvotes: 1

Related Questions