我在将 bash 命令转换为 shell 脚本时遇到错误 shell 脚本中的语法错误
I'm facing an error while converting my bash comand to shell script syntax error in shell script
#!/bin/bash
set -o errexit
set -o nounset
#VAF_and_IGV_TAG
paste <(grep -v "^#" output/""/""_Variant_Filtering/""_GATK_filtered.vcf | cut -f-5) \
<(grep -v "^#" output/""/""_Variant_Filtering/""_GATK_filtered.vcf | cut -f10-| cut -d ":" -f2,3) |
sed 's/:/\t/g' |
sed '1i chr\tstart\tend\tref\talt\tNormal_DP_VCF\tTumor_DP_VCF\tDP'|
awk 'BEGIN{FS=OFS="\t"}{sub(/,/,"\t",);print}' \
> output/""/""_Variant_Annotation/""_VAF.tsv
如果我 运行 在终端中使用此变量而不使用变量,我的上述代码将以语法错误结束,它不会显示语法错误
sh Test.sh S1 Test.sh: 6: Test.sh: 语法错误: "(" unexpected
paste <(grep -v "^#" output/S1/S1_Variant_Filtering/S1_GATK_filtered.vcf | cut -f-5) \
<(grep -v "^#" output/S1/S1_Variant_Filtering/S1_GATK_filtered.vcf | cut -f10-| cut -d ":" -f2,3) |
sed 's/:/\t/g' |
sed '1i chr\tstart\tend\tref\talt\tNormal_DP_VCF\tTumor_DP_VCF\tDP'|
awk 'BEGIN{FS=OFS="\t"}{sub(/,/,"\t",);print}' \
> output/S1/S1_Variant_Annotation/S1_VAF.ts
我的 vcf 文件如下所示:https://drive.google.com/file/d/1HaGx1-3o1VLCrL8fV0swqZTviWpBTGds/view?usp=sharing
如果您尝试 运行 sh
下的此代码,则不能使用 <(command)
进程替换。不幸的是,没有优雅的方法来避免临时文件(或更可怕的东西)但是你的 paste
命令 - 实际上整个管道 - 似乎相当容易重构为 Awk 脚本。
#!/bin/sh
set -eu
awk -F '\t' 'BEGIN { OFS=FS;
print "chr\tstart\tend\tref\talt\tNormal_DP_VCF\tTumor_DP_VCF\tDP' }
!/#/ { p=[=10=]; sub(/^([^\t]*\t){9}/, "", p);
sub(/^[:]*:/, "", p); sub(/:.*/, "", p);
sub(/,/, "\t", p);
s = sprintf("%s\t%s\t%s\t%s\t%s\t%s", , , , , , p);
gsub(/:/, "\t", s);
print s
}' output/""/""_Variant_Filtering/""_GATK_filtered.vcf \
> output/""/""_Variant_Annotation/""_VAF.tsv
在无法访问 VCF 文件的情况下,我无法对此进行测试,但至少它应该为如何继续提供一个大致方向。
sh
不支持 bash
进程替换 <()
。移植它的最简单方法是写出两个临时文件,并在完成时通过陷阱删除它们。更好的选择是使用足够强大的工具(即 sed)来执行所需的过滤和操作:
#!/bin/sh
header="chr\tstart\tend\tref\talt\tNormal_DP_VCF\tTumor_DP_VCF\tDP"
field_1_to_5='\(\([^\t]*\t\)\{5\}\)' # to
field_6_to_8='\([^\t]*\t\)\{4\}[^:]*:\([^,]*\),\([^:]*\):\([^:]*\).*' # to
src="output//_Variant_Filtering/_GATK_filtered.vcf"
dst="output//_Variant_Variant_Annotation/_VAF.tsv"
sed -n \
-e '1i '"$header" \
-e '/^#/!s/'"${field_1_to_5}${field_6_to_8}"'/\t\t/p' \
"$src" > "$dst"
如果您使用的是 awk(或 perl、python 等),只需将脚本移植到该语言即可。
顺便说一句,所有重复的人
建议您重新制定文件命名标准。
#!/bin/bash
set -o errexit
set -o nounset
#VAF_and_IGV_TAG
paste <(grep -v "^#" output/""/""_Variant_Filtering/""_GATK_filtered.vcf | cut -f-5) \
<(grep -v "^#" output/""/""_Variant_Filtering/""_GATK_filtered.vcf | cut -f10-| cut -d ":" -f2,3) |
sed 's/:/\t/g' |
sed '1i chr\tstart\tend\tref\talt\tNormal_DP_VCF\tTumor_DP_VCF\tDP'|
awk 'BEGIN{FS=OFS="\t"}{sub(/,/,"\t",);print}' \
> output/""/""_Variant_Annotation/""_VAF.tsv
如果我 运行 在终端中使用此变量而不使用变量,我的上述代码将以语法错误结束,它不会显示语法错误
sh Test.sh S1 Test.sh: 6: Test.sh: 语法错误: "(" unexpected
paste <(grep -v "^#" output/S1/S1_Variant_Filtering/S1_GATK_filtered.vcf | cut -f-5) \
<(grep -v "^#" output/S1/S1_Variant_Filtering/S1_GATK_filtered.vcf | cut -f10-| cut -d ":" -f2,3) |
sed 's/:/\t/g' |
sed '1i chr\tstart\tend\tref\talt\tNormal_DP_VCF\tTumor_DP_VCF\tDP'|
awk 'BEGIN{FS=OFS="\t"}{sub(/,/,"\t",);print}' \
> output/S1/S1_Variant_Annotation/S1_VAF.ts
我的 vcf 文件如下所示:https://drive.google.com/file/d/1HaGx1-3o1VLCrL8fV0swqZTviWpBTGds/view?usp=sharing
如果您尝试 运行 sh
下的此代码,则不能使用 <(command)
进程替换。不幸的是,没有优雅的方法来避免临时文件(或更可怕的东西)但是你的 paste
命令 - 实际上整个管道 - 似乎相当容易重构为 Awk 脚本。
#!/bin/sh
set -eu
awk -F '\t' 'BEGIN { OFS=FS;
print "chr\tstart\tend\tref\talt\tNormal_DP_VCF\tTumor_DP_VCF\tDP' }
!/#/ { p=[=10=]; sub(/^([^\t]*\t){9}/, "", p);
sub(/^[:]*:/, "", p); sub(/:.*/, "", p);
sub(/,/, "\t", p);
s = sprintf("%s\t%s\t%s\t%s\t%s\t%s", , , , , , p);
gsub(/:/, "\t", s);
print s
}' output/""/""_Variant_Filtering/""_GATK_filtered.vcf \
> output/""/""_Variant_Annotation/""_VAF.tsv
在无法访问 VCF 文件的情况下,我无法对此进行测试,但至少它应该为如何继续提供一个大致方向。
sh
不支持 bash
进程替换 <()
。移植它的最简单方法是写出两个临时文件,并在完成时通过陷阱删除它们。更好的选择是使用足够强大的工具(即 sed)来执行所需的过滤和操作:
#!/bin/sh
header="chr\tstart\tend\tref\talt\tNormal_DP_VCF\tTumor_DP_VCF\tDP"
field_1_to_5='\(\([^\t]*\t\)\{5\}\)' # to
field_6_to_8='\([^\t]*\t\)\{4\}[^:]*:\([^,]*\),\([^:]*\):\([^:]*\).*' # to
src="output//_Variant_Filtering/_GATK_filtered.vcf"
dst="output//_Variant_Variant_Annotation/_VAF.tsv"
sed -n \
-e '1i '"$header" \
-e '/^#/!s/'"${field_1_to_5}${field_6_to_8}"'/\t\t/p' \
"$src" > "$dst"
如果您使用的是 awk(或 perl、python 等),只需将脚本移植到该语言即可。
顺便说一句,所有重复的人 建议您重新制定文件命名标准。