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

<pre class="lang-sh prettyprint-override"><code>#!/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" &gt; equant.sf
done 
</code></pre>

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

<p>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:</p>

<pre class="lang-sh prettyprint-override"><code>#!/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" &gt; "$outName"
done 
</code></pre>

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

<pre><code><span class="math-container">$ fn=/home/usr/Downloads/salmon-1.2.1_linux_x86_64/quants/SRR/quant.sf8068516
$</span> echo ${fn/quant.sf/equant.sf}
/home/usr/Downloads/salmon-1.2.1_linux_x86_64/quants/SRR/equant.sf8068516
</code></pre>

<p>This means that your awk command will become:</p>

<pre><code>awk 'BEGIN { OFS=FS="\t" } { sub("\\..*", "", $1); print }' /home/usr/Downloads/salmon-1.2.1_linux_x86_64/quants/SRR/quant.sf8068516 &gt; /home/usr/Downloads/salmon-1.2.1_linux_x86_64/quants/SRR/equant.sf8068516
</code></pre>

CC BY-SA 4.0
2020-05-03T15:59:10.847Z
Thus speaks the oracle of Awk
M__
CC BY-SA 4.0
2020-05-03T14:50:44.673Z
2020-05-03T15:53:41.187Z
2020-05-03T15:53:41.187Z
terdon
<p>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:</p>

<pre><code>    #!/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 <span class="math-container">${samp}"
awk 'BEGIN { OFS=FS="\t" } { sub("\\..*", "", $</span>1); print }' quant.sf &gt; equant.sf
done 
</code></pre>

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

<shell><awk>
awk shell script
# awk shell script

(Asked by: username_0 on 2020-05-03)

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.

**Tags:** shell, awk

---

## 1 answers

### Answer by username_1 on 2020-05-03. Score: 2 | Accepted answer

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

> **Comments:**
> **username_2**: Thus speaks the oracle of Awk  

---

