我有一个文件,其中第一行包含一系列字段,选项卡分隔(\t
)。我试图遍历这些线,并将其中的一些字段用作程序的变量。到目前为止,我掌握的代码如下:
{
A=$(head -1 id_table.txt)
read;
while IFS='\t' read $A;
do
echo 'downloading '$SRA_Sample_s
echo $tissue_s
#out_dir=`echo $tissue_s | sed 's/ /./g'` #Replacing spaces by dots
#/soft/bio/sequence/sratoolkit-2.3.4-2/bin/fastq-dump.2.3.4 --split-3 --outdir $out_dir --ncbi_error_report $SRA_Sample_s
done
} <./id_table.txt
输出(错):
downloading _s Inser
downloading provided> <no
downloading provided> <no
downloading provided> <no
它失败了,因为它没有正确地获取字段。也许<>
字符造成了混乱?不同的文件以不同的顺序排列列的名称,某些文件中缺少一些列。我被困在这里了。
该文件如下所示:
BioSample_s MBases_l MBytes_l Run_s SRA_Sample_s Sample_Name_s age_s breed_s sex_s Assay_Type_s AssemblyName_s BioProject_s BioSampleModel_s Center_Name_s Consent_s InsertSize_l Library_Name_s Platform_s SRA_Study_s biomaterial_provider_s g1k_analysis_group_s g1k_pop_code_s source_s tissue_s
SAMN02777951 4698 3249 SRR1287653 SRS607026 SL01 19 SL01 female RNA-Seq <not provided> PRJNA247712 Model organism or animal SICHUAN UNIVERSITY public 200 <not provided> ILLUMINA SRP041998 Chengdu Research Base of Giant Panda Breeding <not provided> <not provided> <not provided> blood
SAMN02777952 4451 3063 SRR1287654 SRS607028 XB01 12 XB01 male RNA-Seq <not provided> PRJNA247712 Model organism or animal SICHUAN UNIVERSITY public 200 <not provided> ILLUMINA SRP041998 Chengdu Research Base of Giant Panda Breeding <not provided> <not provided> <not provided> blood
SAMN02777953 4553 3139 SRR1287655 SRS607025 XB02 6 XB02 female RNA-Seq <not provided> PRJNA247712 Model organism or animal SICHUAN UNIVERSITY public 200 <not provided> ILLUMINA SRP041998 Chengdu Research Base of Giant Panda Breeding <not provided> <not provided> <not provided> blood
发布于 2014-12-01 14:04:16
您可能会发现,与shell循环相比,awk脚本更健壮,使用起来也更不麻烦:
$ cat tst.awk
BEGIN { FS="\t" }
NR==1 { for (i=1; i<=NF; i++) f[$i]=i; next }
{
print "downloading", $(f["SRA_Sample_s"])
out_dir = $(f["tissue_s"])
gsub(/ /,".",out_dir)
cmd = sprintf( "/soft/bio/sequence/sratoolkit-2.3.4-2/bin/fastq-dump.2.3.4 --split-3 --outdir %s --ncbi_error_report %s", out_dir, $(f["SRA_Sample_s"]) )
print cmd
#system(cmd); close(cmd)
}
。
$ awk -f tst.awk file
downloading SRR1287653
/soft/bio/sequence/sratoolkit-2.3.4-2/bin/fastq-dump.2.3.4 --split-3 --outdir blood --ncbi_error_report SRR1287653
downloading SRR1287654
/soft/bio/sequence/sratoolkit-2.3.4-2/bin/fastq-dump.2.3.4 --split-3 --outdir blood --ncbi_error_report SRR1287654
downloading SRR1287655
/soft/bio/sequence/sratoolkit-2.3.4-2/bin/fastq-dump.2.3.4 --split-3 --outdir blood --ncbi_error_report SRR1287655
我想说的是,如果没有调用外部命令,那么应该避免shell循环,这样做的不仅仅是文本处理。
另外,考虑使用awk进行文本处理,然后转到shell循环执行外部命令:
$ cat tst.awk
BEGIN { FS=OFS="\t" }
NR==1 { for (i=1; i<=NF; i++) f[$i]=i; next }
{
gsub(/ /,".",$(f["tissue_s"]))
print $(f["tissue_s"]), $(f["SRA_Sample_s"])
}
。
$ awk -f tst.awk file |
while IFS=$'\t' read -r out_dir SRA_Sample_s
do
printf 'downloading %s\n' "$SRA_Sample_s"
#/soft/bio/sequence/sratoolkit-2.3.4-2/bin/fastq-dump.2.3.4 --split-3 --outdir $out_dir --ncbi_error_report $SRA_Sample_s
done
downloading SRR1287653
downloading SRR1287654
downloading SRR1287655
发布于 2014-12-01 15:15:28
IFS='\t'
没有按你想的方式工作。这是由t
划分的。使用IFS=$'\t'
使用选项卡。
这就是为什么你要得到_s Inser
等等(请注意,它在字母t
时开始并切断)。
尽管如此,我完全同意EdMorton的观点,即使用awk来实现这一点可能是一个更好的主意,尽管我相信如果谨慎引用并断言选项卡不会出现在输入文件中,您很可能只使用shell就可以安全地做到这一点(但是Ed已经不止一次向我展示了我最初想法的错误,所以他很可能在想我没有想到的事情)。
发布于 2014-12-01 14:05:00
尝试(根据你的发展风格)
cat id_table.txt \
| {
read Header
while eval "read ${Header}"
do
echo "Donwloading ${SRA_Sample_s}"
echo "${tissue_s}"
done
}
https://stackoverflow.com/questions/27227735
复制相似问题