1

I am using an array of values, and I want to look for those values using awk and output to file. In the awk line if I replace the first "$i" with the numbers themselves, the script works, but when I try to use the variable "$i" the script no longer works.

declare -a arr=("5073770" "7577539")
for i in "${arr[@]}"
do
    echo "$i"

    awk -F'[;\t]' '$2 ~ "$i"{sub(/DP=/,"",$15); print $15}' $INPUT >> "$i"
done

The file I'm looking at contains many lines like the following:

chr12   3356475 .   C   A   76.508  .   AB=0;ABP=0;AC=2;AF=1;AN=2;AO=3;CIGAR=1X;DP=3;DPB=3;DPRA=0;EPP=9.52472;EPPR=0;GTI=0;LEN=1;MEANALT=1;MQM=60;MQMR=0;NS=1;NUMALT=1;ODDS=8.76405;PAIRED=0;PAIREDR=0;PAO=0;PQA=0;PQR=0;PRO=0;QA=111;QR=0;RO=0;RPP=9.52472;RPPR=0;RUN=1;SAF=3;SAP=9.52472;SAR=0;SRF=0;SRP=0;SRR=0;TYPE=snp GT:DP:RO:QR:AO:QA:GL    1/1:3:0:0:3:111:-10,-0.90309,0
The Nightman
  • 5,609
  • 13
  • 41
  • 74
  • This would benefit from some example data. (Input and output desired). Also: Does it have to be bash/awk? – Sobrique Jan 14 '16 at 19:37
  • It doesn't have to be bash or awk. I am getting the values equal to `DP` from many files that are at a particular numberical location. – The Nightman Jan 14 '16 at 19:38

3 Answers3

1

Pass the value $i to awk using -v:

awk -F'[;\t]' -v var="$i" '$2 ~ var{sub(/DP=/,"",$15); print $15}' $INPUT >> "$i"
P.P
  • 117,907
  • 20
  • 175
  • 238
1

awk will have no idea what the value of the shell's $i is unless you explicitly pass it into awk as a variable

awk  -F'[;\t]' -v "VAR=${i}"  '$2 ~ VAR {....

I expect the result you see is because 'i' is undefined and treated as zero which makes your test '$2 ~ $0 {...

tomc
  • 1,146
  • 6
  • 10
1

You can avoid awk and do this in BASH itself:

arr=("5073770" "7577539" "3356475")

for i in "${arr[@]}"; do
   while IFS='['$'\t'';]' read -ra arr; do
      [[ ${arr[1]} == *$i* ]] && { s="${arr[14]}"; echo "${s#DP=}"; }
   done < "$INPUT"
done
anubhava
  • 761,203
  • 64
  • 569
  • 643