给定一个充满 .sam 文件的目录,对于每个文件:
如果column3=blah且451000 =<column4 <= 468999,则将行写入file_ribos.sam
否则,将行写入 file_non_ribos.sam
输入示例
# file_1.sam
abc 123 blah 451200
abc 123 blah 450999
示例输出
# file_1_ribos.sam
abc 123 blah 451200
# file_1_non_ribos.sam
abc 123 blah 450999
我的代码似乎失败了。我究竟做错了什么?
for file in *.sam ; do
awk -F"\t" '
{if($3 == "blah" && $4 >= "451000" && $4 <= "468999") {
{print $0} > "$(basename "$file" .sam)_ribos.sam";}
else {print $0} > "$(basename "$file" .sam)_non_ribos.sam";}
' $file;
done
答案1
我稍微重构了你的代码......
for file in *.sam ; do
awk -v basename="$(basename $file .sam)" '
{ non = ($3 == "blah" && $4 >= 451000 && $4 <= 468999) ? "" : "_non"
outfile = basename non "_ribos.sam"
print > outfile
}
' "$file"
done