baracus37 baracus37 - 1 year ago 69
Linux Question

output filename when running python script over multiple files

I am using the Tracer software package (
The program is invoked as followed:

tracer assemble [options] <file_1> [<file_2>] <cell_name> <output_directory>

The program runs on a single dataset and the output goes to

What I want to do now is run this program on multiple files. To do so this is what I do:

for filename in /home/tobias/tracer/datasets/test/*.fastq
echo "Processing $filename file..."
python tracer assemble --single_end --fragment_length 62 --fragment_sd 1 $filename Tcell_test output;

This works in priciple, but as
is static, every iteration overwrites the output from the previous iteration. How do I need to change my script in order to give the output folder the name of the input file?

For example: Input filename is
. For this cell_name should be
. Next file is
and cell_name should be
, and so on...

cxw cxw
Answer Source

I think this will do it, in bash, if I understand correctly -

for filename in /home/tobias/tracer/datasets/test/*.fastq
  echo "Processing $filename file..."
  basefilename="${filename##*/}"   #<--- 
  python tracer assemble --single_end --fragment_length 62 --fragment_sd 1 "$filename" "${basefilename%.fastq}" output;
  #                                                                                    ^^^^^^^^^^^^^^^^^^^^^^^^

${filename##*/} removes the part up to the last /, and ${basefilename%.fastq} removes the .fastq at the end.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download