0

I wasn't sure how to word this question but basically I have been using a shell script to process sam data. Here's the shell script.

nano picard_SortandIndex_from_sam.sh

for A in *sam; do ls $A; picard SortSam I=$A O=$A".bam" SO=coordinate CREATE_INDEX=TRUE; done 

And this is the command I run where I get only one .out file for all the sam files processed.

nohup sh picard_SortandIndex_from_sam.sh > picard_SortandIndex_from_sam5_26_21.out &

Is there a way to generate .out files within my "picard_SortandIndex_from_sam.sh" script so I can have a summary file for each sam file processed? I tried to put > $A".out" in there but it didn't work...

Many thanks!

4
  • Does this answer your question? Combine the output of two commands in bash
    – Panki
    Sep 8, 2021 at 15:38
  • If you want the ls output aswell, wrap the part between do and done like shown above, then redirect that ( { ls [...], picard [...] } >"$A".out)
    – Panki
    Sep 8, 2021 at 15:41
  • 1
    "I tried to put > $A".out" in there but it didn't work..." -- what does this mean, exactly? What did you try exactly? How did it not work? What happened instead?
    – ilkkachu
    Sep 8, 2021 at 15:48
  • @ilkkachu it actually worked but somehow the size of the new files didn't change until the whole step was finished. I thought it wasn't working. Turned out I just had to wait. Thanks!
    – Xiu
    Sep 8, 2021 at 18:43

1 Answer 1

1

I think you are looking for this:

for A in *sam; do 
  ls "$A"
  picard SortSam I="$A" O="$A".bam SO=coordinate CREATE_INDEX=TRUE > "$A".out
done

Or, more likely, you want both standard error and standard output, so use this:

for A in *sam; do 
  ls "$A"
  picard SortSam I="$A" O="$A".bam SO=coordinate CREATE_INDEX=TRUE > "$A".out 2>&1
done

This will run picard on each of your sam files, and store the output of the command (> "$A".out) and any errors produced (2>&1) in the file $A.out.

As a final note, since this creates files named foo.sam.bam and foo.sam.bam.bai etc., you might want to remove the .sam from the output name:

 for A in *sam; do 
  ls "$A"
  fileName=$(basename "$A" .sam)
  picard SortSam I="$A" O="$fileName".bam SO=coordinate CREATE_INDEX=TRUE > "$fileName".out 2>&1
done

That will create foo.bam from foo.sam instead of foo.sam.bam.

1
  • This worked!! Thank you so much!
    – Xiu
    Sep 8, 2021 at 18:39

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .