I wrote some unix code that performs arithmetic on my files and spits out a data matrix into analysis.txt (with added headers). However, when I tried to put this code into a script and run the script, the tabs (\t) separate my columns rather than actual whitespace.
Code that works:
for f in */Data/Intensities/BaseCalls/Alignment*/*.bam; do
echo 'Processing '$f' ... ' 1>&2
name=${f##*/}
name=${name%.bam}
pftot=`samtools view -c $f`
pfmit=`samtools view -c $f chrM`
pfgen=$((pftot-pfmit))
pfratio=`python -c 'print float('$pfmit')/'$pfgen`
ftot=`samtools view -c -q 30 -F 1536 $f`
fmit=`samtools view -c -q 30 -F 1536 $f chrM`
fgen=$((ftot - fmit))
fratio=`python -c 'print float('$fmit')/'$fgen`
echo $name'\t'$pftot'\t'$pfmit'\t'$pfgen'\t'$pfratio'\t'$ftot'\t'$fmit'\t'$fgen'\t'$fratio
done | awk 'BEGIN{print
"name\ttotal\tmDNA\tchrDNA\tratio\tftotal\tfmDNA\tfchrDNA\tfratio"}{print}'
> Analysis.txt
Code that doesn't work:
#!/bin/bash
for f in */Data/Inten... "the above code"
Run with:
bash Analysis.sh
prints the variables in the last echo line with literal "\t" separators.
I'm rather new, so any suggestions or resources are appreciated.