grepping variables containing special characters i

2019-08-10 21:36发布

问题:

I am trying to grep out some lines from a file based on patterns stored in a variable in bash script that may contain (, ), [ or ]. I get the desired output with patterns that do not contain the special characters but with ( or ), I get a blank output and with [ or ], I get the following error: grep: range out of order in character class

Sample of pattern file:

14-3-3-like protein B
14-3-3-like protein B (Fragment)
3-oxoacyl-[acyl-carrier-protein] synthase 2

Sample of input file:

seq1    gi|703124372    380    285    +    2e-154    14-3-3-like protein B    sp
seq2    Q96451    69    51    +    3e-16    14-3-3-like protein B (Fragment)    sp
seq3    P0AAI5    104    84    -    4e-20    3-oxoacyl-[acyl-carrier-protein] synthase 2    sp

My code is as below:

if [ $@==0 ]
    then echo -e "\nUSAGE: $0 [pattern file] [in file] > [out file]\n"
    exit;
else
    while read line; do
            echo -e "Pattern: $line"
            grep -P "\t$line\t" $2
            echo -e "\n"
    done < $1

Sample of the output:

Pattern: 14-3-3-like protein B 
seq1    gi|703124372    380    285    +    2e-154    14-3-3-like protein B    sp
Pattern: 14-3-3-like protein B (Fragment)    sp
Pattern: 3-oxoacyl-[acyl-carrier-protein] synthase 2
grep: range out of order in character class

I've tried using grep -Fw but that also doesn't give the desired output.. I've also tried substituting the patterns in the two input files with \( and \[ instead of ( and [ but that also doesn't work..

Any idea how can I achieve this? Is there anything else I could use instead of grep?

回答1:

tab=$(echo -e \\t)
grep -F "$tab$line$tab" $2

Edit: See also the suggestion from @anubhava: grep -F $'\t'"$line"$'\t' "$2"