我有一个如下所示的文本文件:
UICEX_0001 UICEX_0001_T1.bam UICEX_0001_C2.bam chr1:16946335 chr19:9064309 chr8:10480278
UICEX_0003 UICEX_0003_T1.bam UICEX_0003_C2.bam chr1:16974893
我正在尝试使用此信息来创建类似以下内容的内容,组合字符串并从文本文件动态输入信息。我想要:
- 循环遍历每一行
- 将前三列分配给变量
- 使用这些变量打印一些文本
- 然后循环遍历第四端列
- 打印一些具体的东西。
这是我的输出文件的示例:
输出文件
load UICEX_0001_T1.bam
load UICEX_0001_C2.bam
goto chr1:16946335
collapse
snapshot UICEX_0001_chr1:16946335.png
goto chr19:9064309
collapse
snapshot UICEX_0001_chr19:9064309.png
goto chr8:10480278
collapse
snapshot UICEX_0001_chr8:10480278.png
load UICEX_0003_T1.bam
load UICEX_0003_C2.bam
collapse
snapshot UICEX_0003_chr1:16974893.png
我尝试过的
我想我可以通过嵌套 gawk 命令来做到这一点。这是我尝试过的一个:
SAMPLEFILE = "2016-10-13_mutation_table.txt"
gawk -F";" 'BEGIN{
gawk -F";" -v SAMPLE=$(cat $SAMPLEFILE | cut -d"\t" -f1) -v BAMT=$(cat $SAMPLEFILE | cut -d"\t" -f2) -v BAMN=$(cat $SAMPLEFILE | cut -d"\t" -f3);}{print "new \nload " $BAMN;}
{print "new \nload " $BAMT;}
{awk "{for(i=4; i<=NF-1; i++){ print "goto " $i ; print "collapse\nsnapshot " $SAMPLE"_"$i".png";} }" 2016-10-13_mutation_table.txt;
}END{print "exit \n"}'
但运行此命令会出现如下错误:
gawk: cmd. line:2: gawk -F";" -v SAMPLE=$(cat $SAMPLEFILE | cut -d"\t" -f1) ...
gawk: cmd. line:2: ^ syntax error
答案1
try this
$ awk '{printf("load %s\nload %s\n",$2,$3);for(i=4;i<=NF;i++){printf("goto %s\ncollapse\nsnapshot %s_%s.png\n",$i,$1,$i)}}' test.t
load UICEX_0001_T1.bam
load UICEX_0001_C2.bam
goto chr1:16946335
collapse
snapshot UICEX_0001_chr1:16946335.png
goto chr19:9064309
collapse
snapshot UICEX_0001_chr19:9064309.png
goto chr8:10480278
collapse
snapshot UICEX_0001_chr8:10480278.png
load UICEX_0003_T1.bam
load UICEX_0003_C2.bam
goto chr1:16974893
collapse
snapshot UICEX_0003_chr1:16974893.png
答案2
你什么似乎要求可以这样做
awk '
NR > 1 {print ""}
{
printf("load %s\nload %s\n", $2, $3);
for (i=4; i<=NF; i++) {
if (NF > 4) printf("goto %s\n", $i);
printf("collapse\nsnapshot %s_%s.png\n", $1, $i);
}
}
' samplefile