在管道 awk 输出上打印循环输入文件名
Print for loop input filename on piped awk output
我希望在 awk 的每一行输出以下内容:
for fname in $(ls *.bam | cut -c -9 | uniq)
do
echo $fname
samtools depth -a "$fname".bam | awk '{sum+=} END {print "Average = ", sum/NR}' >> mean.depth
done
当前输出:
$ less mean.depth
Average = 5.40089
Average = 4.68387
期望的输出:
$ less mean.depth
FILENAME1 Average = 5.40089
FILENAME2 Average = 4.68387
这样输出的平均深度是可追踪的。我试过 FILENAME 和各种各样的东西但没有成功。谢谢!
解决方案
改编自this answer
用awk -v var=
定义shell变量$fname,然后用END {print var
打印它
samtools depth -a "$fname".bam | awk -v var=$fname '{sum+=} END {print var, " = ", sum/NR}' >> mean.depth
我希望在 awk 的每一行输出以下内容:
for fname in $(ls *.bam | cut -c -9 | uniq)
do
echo $fname
samtools depth -a "$fname".bam | awk '{sum+=} END {print "Average = ", sum/NR}' >> mean.depth
done
当前输出:
$ less mean.depth
Average = 5.40089
Average = 4.68387
期望的输出:
$ less mean.depth
FILENAME1 Average = 5.40089
FILENAME2 Average = 4.68387
这样输出的平均深度是可追踪的。我试过 FILENAME 和各种各样的东西但没有成功。谢谢!
解决方案
改编自this answer
用awk -v var=
定义shell变量$fname,然后用END {print var
samtools depth -a "$fname".bam | awk -v var=$fname '{sum+=} END {print var, " = ", sum/NR}' >> mean.depth