This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Print (awk) and echo (bash) together to get variable in one row

Dear all, I create some pipe to get the information from the text file.

#!/bin/bash

for i in {0..249480000..60000}
do
    u=$i
    let "u +=60000"
    echo $i "-" $u
    samtools view /home/filip/Desktop/54321Odfiltrovany.bam  chrY:$i-$u | awk '{ n=length($10); print gsub(/[GCCgcs]/,"",$10)/n;}' | awk '{s+=$1}END{print NR,s/NR}'
done

OUTPUT what I want:

$i $u $NR $s/NR

for example:

0 60000 2 0.345
60000 120000 3 0.45

Could you help me. Because I do not know how could I join echo with print. Thank you.

awk join print echo bash

1 answer

Sometimes a strange battle can take place between shell and awk, where we want to allow some shell variables to pass into awk but we want to protect the awk variables from the shell.

The right solution is usually to rewrite your script to pass variables into awk with the -v flag like so:

$ export VALUE=100
​$ echo $HOME | awk -v shift=100 -v maxval=$VALUE  '{ print shift + maxval }'

​Produces

200

Log in to answer this question.