![对多个文件运行一系列命令](https://linux22.com/image/186997/%E5%AF%B9%E5%A4%9A%E4%B8%AA%E6%96%87%E4%BB%B6%E8%BF%90%E8%A1%8C%E4%B8%80%E7%B3%BB%E5%88%97%E5%91%BD%E4%BB%A4.png)
我有以下文件:
FC_014_6361.bam
FC_014_6462.bam
FC_014_6183.bam
目前,我对每个文件分别运行以下命令。例如FC_014_6361.bam
命令是:
$ bedtools intersect -abam FC_014_6361.bam -b CLIA-bed-for-DRAGEN_1.bed > FC_014_6361Inter.bam
$ samtools sort FC_014_6361Inter.bam > FC_014_6361InterSort.bam
$ samtools depth -a FC_014_6361InterSort.bam -b CLIA-bed-for-DRAGEN_1.bed > FC_014_6361InterSortDepth.txt
$ awk '$3<20' FC_014_6361InterSortDepth.txt > 6361_20.txt
对许多这种类型的文件执行此操作非常乏味。有人可以帮助编写一个脚本来自动执行此操作并对其进行解释吗?
答案1
对于每个源文件名,您可以稍微修改它并将结果保存在以原始文件命名的文件中。使用这些信息我们可以构建一个循环。
将以下内容放入文件中,例如doit
#!/bin/bash
#
for bamsrc in "$@" # For each item on the command line
do
prefix="${bamsrc%.bam}" # Strip off trailing .bam
result="${prefix##*_}" # Strip off leading *_
echo "Processing '$bamsrc' into intermediate bam files and ${result}_20.txt"
# Perform the processing
bedtools intersect -abam "$bamsrc" -b CLIA-bed-for-DRAGEN_1.bed > "${prefix}Inter.bam"
samtools sort "${prefix}Inter.bam" >"${prefix}InterSort.bam"
samtools depth -a "${prefix}InterSort.bam" -b CLIA-bed-for-DRAGEN_1.bed > "${prefix}InterSortDepth.txt"
awk '$3<20' "${prefix}InterSortDepth.txt" > "${result}_20.txt"
done
使脚本文件可执行
chmod a+x doit
然后使用源文件名作为空格分隔列表运行它
./doit FC_014_6361.bam FC_014_6462.bam FC_014_6183.bam
答案2
首先是这里。尚未测试它,但它可能会帮助您创建解决方案。
#!/bin/bash
for filename in *.bam;do
number=$( echo $filename | grep -o '[0-9]\+' )
bedtools intersect -abam $filename -b CLIA-bed-for-DRAGEN_1.bed > "$filename"Inter.bam
samtools sort "$filename"Inter.bam > "$filename"InterSort.bam
samtools depth -a "$filename"InterSort.bam -b CLIA-bed-for-DRAGEN_1.bed > "$filename"InterSortDepth.txt
awk '$3<20' "$filename"InterSortDepth.txt > "$number"_20.txt
done