0

I have a text file that looks like this:

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

I am trying to use this information to create something like the following, combining strings and dynamically inputting information from the text file. I want to:

  • loop through each line
  • assign the first three columns to variables
  • print some text with those variables
  • then loop through the 4th-end column
  • print something specific.

Here's an example of my output file:

Output File

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

What I've tried

I thought I could do this by nesting gawk commands. Here's one I've tried:

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"}'

But running this gets errors such as this:

gawk: cmd. line:2: gawk -F";" -v SAMPLE=$(cat $SAMPLEFILE | cut -d"\t" -f1) ...
gawk: cmd. line:2:                     ^ syntax error
  • ... hard to say where to start. This code is a real mess. You are embedding two separate awk one-liners inside an awk script. Try formatting your own code with more whitespace and indentation, and you might see some of the problems. – glenn jackman Oct 14 '16 at 0:51
  • Also, in shell, there must not be any spaces around the = in a variable assignment – glenn jackman Oct 14 '16 at 0:52
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
1

What you seem to be asking for can be done as

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.