
我有一個如下所示的文本文件:
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