1

I am working in a directory with file names ending with fastq.gz. with using a loop like the following, I will be running a tool.

for i inls; do if [[ "$i" == *".gz" ]]; then bwa aln ../hg38.fa $i > $i | sed 's/fastq.gz/sai/g'; fi; done

My question is, I want my output filename to end with .sai instead of fastq.gz with keeping the rest of the filename the same. yet, as it first sees $i after >, it modifies the input file itself. I tried using it like <($i | sed 's/fastq.gz/sai/g') but that does not work either. what is the right way of writing this?

1 Answer 1

2

You can use string replacements to compute the filename and the extension. Moreover, you shouldn't rely on the ls output but loop directly over the expression you are looking for.

 for file in *.gz; do
   name="${file%.*}"
   file_output="${name}.sai"
   bwa aln ../hg38.fa ${file} > ${file_output}
 done
Sign up to request clarification or add additional context in comments.

2 Comments

this is a very good way and solves my issue, thanks. just out of curiosity, do you also know how to give the preference to sed on $i and get the modified $i as in the question?
Well I will always do it in several steps since it is a script and you do not need to compess it but you could do: CMD > $(echo $file | sed 's/fastq.gz/sai/g')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.