Search code examples
shellvariablesgrepnumberssubtraction

Subtracting a number from a variable throwing an error


I am using following code to grep the values that are calculated by adding and subtracting 1 from 2nd and 3rd column as follows:

 while read intron ; do
   start=$(grep -w $intron ../file_to_be_grepped_from | awk '{print $2}')
   end=$(grep -w $intron ../file_to_be_grepped_from | awk '{print $3}')
   str=$(($start-1))
   en=$(($end+1))
   grep -w $str ../file_to_be_grepped_from
   grep -w $en ../file_to_be_grepped_from
   echo $intron 
  done <  file

However the part where I am adding and subtracting 1 from 3rd and 2nd column respectively , I am getting an error as "46844234-1: syntax error in expression (error token is "46844234-1")" for a line in the file I am grepping from (file_to_be_grepped_from). The lines in the file from which they are grepped look like this:

  chr9  46844104    46844233    ENSG00000226007:E2  +
  chr9  46844104    46846023    ENSG00000226007:I2  +
  chr3  21584308    21584487    ENSG00000225542:E1  +
  chr3  21620681    21620873    ENSG00000225542:E2  +

I am trying to figure out if the error is because of duplicate rows or something else, I tried all the possible syntax for subtracting a number from the variable.


Solution

  • str=$(grep -w $intron ../file_to_be_grepped_from | awk '{print($2-1)}')
    en=$(grep -w $intron ../file_to_be_grepped_from | awk '{print($3+1)}')