title: pass bash variable to awk in order to extract characters at certain positions
body: <p>I am extracting characters from string at certain positions with this command:</p>
  
  <pre><code>left=1; right=4; grep ETM21CH01EPU3Z -A1 SLf_454.fna | tail -n 1 | awk '{print substr($0,1,4)}'
  </code></pre>
  
  <p>and getting this correct output:</p>
  
  <pre><code>GTTG
  </code></pre>
  
  <p>Now I would like to replace <strong>1</strong> and <strong>4</strong> by values in bash variables <strong>left</strong> and <strong>right</strong>.</p>
  
  <p>I tried this:</p>
  
  <pre><code>left=1; right=4; grep ETM21CH01EPU3Z -A1 SLf_454.fna | tail -n 1 | awk -v l=$left r=$right '{print substr($0,l,r)}'
  </code></pre>
  
  <p>awk: cmd. line:1: fatal: cannot open file `{print substr($0,l,r)}' for reading</p>
  
  <p>Any suggestions, please? Thanks.</p>
  
  <p>EDIT: copied from comment below so we can actually read it:</p>
  
  <p>Well, I also have a file, where is written what to search (for example ETM21CH01EPU3Z) and at which positions. So the whole code is:</p>
  
  <pre><code>while read -r line; do
     echo $line;
     name=`echo $line | cut -d' ' -f 1`;
     left=`echo $line | cut -d' ' -f 2`;
     right=`echo $line | cut -d' ' -f 3`;
     grep $name -A1 SLf_454.fna | tail -n 1 | awk -v l=$left -v r=$right '{print substr($0,l,r)}';
  done &lt; CL6o_blastoutput_SLf_454.fna_out 
  </code></pre>