1
$\begingroup$

I have a folder named quant. Inside this folder I have 27 folders named SRR8068516_quant to SRR8068543_quant. I want to edit a file inside these folders titled quant.sf and produce an output. I attempted writing a shell script to do so:

    #!/bin/bash
for fn in /home/usr/Downloads/salmon-1.2.1_linux_x86_64/quants/SRR/quant.sf{8068516..8068543};
do
samp=`basename ${fn}`
echo "Processing sample ${samp}"
awk 'BEGIN { OFS=FS="\t" } { sub("\\..*", "", $1); print }' quant.sf > equant.sf
done 

When I run the script it gives me an error saying : Processing sample quant.sf8068516 awk: cannot open quant.sf (No such file or directory). What is wrong that I am doing. Kindly help.

$\endgroup$
2
$\begingroup$

You are running the command on the same file, quant.sf each time. You need to use your $fn variable instead:

#!/bin/bash
for fn in /home/usr/Downloads/salmon-1.2.1_linux_x86_64/quants/SRR/quant.sf{8068516..8068543};
do
    samp=`basename ${fn}`
    echo "Processing sample ${samp}"
    awk 'BEGIN { OFS=FS="\t" } { sub("\\..*", "", $1); print }' "$fn" > equant.sf
done 

However, that would also fail since it will be simply overwriting a file called equant.sf in your current directory with the results of processing each file.

I can't be sure, since you haven't actually shown us your file structure, but I suspect what you're looking for is actually this:

#!/bin/bash
for fn in /home/usr/Downloads/salmon-1.2.1_linux_x86_64/quants/SRR/quant.sf{8068516..8068543};
do
    samp=`basename ${fn}`
    outName="${fn/quant.sf/equant.sf}"
    echo "Processing sample ${samp}"
    awk 'BEGIN { OFS=FS="\t" } { sub("\\..*", "", $1); print }' "$fn" > "$outName"
done 

The outName="${fn/quant.sf/equant.sf}" replaces the first occurrence of quant.sf in the variable $fn with equant.sf, keeping the path and thereby ensuring that the output of each file is stored in the same directory with the name equant.sfNNNNNN. For example:

$ fn=/home/usr/Downloads/salmon-1.2.1_linux_x86_64/quants/SRR/quant.sf8068516
$ echo ${fn/quant.sf/equant.sf}
/home/usr/Downloads/salmon-1.2.1_linux_x86_64/quants/SRR/equant.sf8068516

This means that your awk command will become:

awk 'BEGIN { OFS=FS="\t" } { sub("\\..*", "", $1); print }' /home/usr/Downloads/salmon-1.2.1_linux_x86_64/quants/SRR/quant.sf8068516 > /home/usr/Downloads/salmon-1.2.1_linux_x86_64/quants/SRR/equant.sf8068516
$\endgroup$
1
  • 1
    $\begingroup$ Thus speaks the oracle of Awk $\endgroup$ – M__ May 3 '20 at 15:59

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.