I am extracting characters from string at certain positions with this command:
left=1; right=4; grep ETM21CH01EPU3Z -A1 SLf_454.fna | tail -n 1 | awk '{print substr($0,1,4)}'
and getting this correct output:
GTTG
Now I would like to replace 1 and 4 by values in bash variables left and right.
I tried this:
left=1; right=4; grep ETM21CH01EPU3Z -A1 SLf_454.fna | tail -n 1 | awk -v l=$left r=$right '{print substr($0,l,r)}'
awk: cmd. line:1: fatal: cannot open file `{print substr($0,l,r)}’ for reading
Any suggestions, please? Thanks.
EDIT: copied from comment below so we can actually read it:
Well, I also have a file, where is written what to search (for example ETM21CH01EPU3Z) and at which positions. So the whole code is:
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 < CL6o_blastoutput_SLf_454.fna_out
You’re missing a
-vbefore yourrightvariable. Every variable that you want to pass intoawkmust be preceded by a-v.Change it to: