Bash loop one liner
1
0
Entering edit mode
2.7 years ago
matt81rd ▴ 10

I am performing a bash loop one liner:.

for k in */centrifuge/kreport/*.txt; do cat $k | cut -f 1,3 > /krona/$k.krona; echo $k; done

The file hierarchy is as follows:

\Drain_4 
    \centrifuge
        \kreport
           A6_kreport.txt
           A4_kreport.txt
           A5_kreport.txt
           \krona
\Drain_5
\Drain_7

Where \ denotes a directory. This is the error i am getting:

-bash: /krona/Drains_2/centrifuge/kreport/A4_kreport.txt: No such file or directory

I am trying to output the file into the krona directory which is in the working directory however when i try to i get that error. When i remove the "/krona" after > the code execute fine so guessing i am having problems with telling it where to output the file but i can't work it out. Any help would be greatly appreciated.

liner loop centrifuge bash • 924 views
ADD COMMENT
0
Entering edit mode

Do you actually need /krona/ (in the root folder) or rather just krona/ in the current directory?

ADD REPLY
0
Entering edit mode

Ideally i would like a folder in the same directory as the three kreport.txt files which contains the results (the altered version of these three files). Drain_5 and Drain_7 also have the same file structure so hopefully the loop will produce the same result as well.

ADD REPLY
1
Entering edit mode
2.7 years ago
iraun 6.2k

$k variable contains the full path to the input file you are parsing. Doing something like this (not tested) should fix the problem:

name=$(basename $k)

And then

> /krona/$name.krona

Try this:

for k in */centrifuge/kreport/*.txt; do
 name=$(basename $k);
 cat $k | cut -f 1,3 > /krona/$name.krona;
 echo $k;
 done
ADD COMMENT
0
Entering edit mode

Hey thanks for your comment!

(note i realised i was working on the wrong results folder now in "results" instead of "kreport" which is why there is a change to the file and folder names but the problem is still the same)

I tried out your suggestion:

for k in */centrifuge/results/*.txt; do name=$(basename $k); cat $k | cut -f 1,3 > /krona/$name.krona; echo $k; done

but am still getting the following error:

    -bash: /krona/A4_results.txt.krona: No such file or directory
Drains_2/centrifuge/results/A4_results.txt
-bash: /krona/B1_results.txt.krona: No such file or directory
Drains_2/centrifuge/results/B1_results.txt
-bash: /krona/B6_results.txt.krona: No such file or directory
Drains_2/centrifuge/results/B6_results.txt
-bash: /krona/D1_results.txt.krona: No such file or directory
Drains_2/centrifuge/results/D1_results.txt
-bash: /krona/E4_results.txt.krona: No such file or directory
Drains_2/centrifuge/results/E4_results.txt
-bash: /krona/NC_D2_results.txt.krona: No such file or directory
Drains_2/centrifuge/results/NC_D2_results.txt
-bash: /krona/C4_results.txt.krona: No such file or directory
Drains_4/centrifuge/results/C4_results.txt
-bash: /krona/D3_results.txt.krona: No such file or directory
Drains_4/centrifuge/results/D3_results.txt
-bash: /krona/E7_results.txt.krona: No such file or directory
Drains_4/centrifuge/results/E7_results.txt
-bash: /krona/G1_results.txt.krona: No such file or directory
Drains_4/centrifuge/results/G1_results.txt
-bash: /krona/I1_results.txt.krona: No such file or directory
Drains_4/centrifuge/results/I1_results.txt
-bash: /krona/I8_results.txt.krona: No such file or directory
Drains_4/centrifuge/results/I8_results.txt
-bash: /krona/NC_D4_results.txt.krona: No such file or directory
Drains_4/centrifuge/results/NC_D4_results.txt
-bash: /krona/A7_results.txt.krona: No such file or directory
Drains_6/centrifuge/results/A7_results.txt
-bash: /krona/B4_results.txt.krona: No such file or directory
Drains_6/centrifuge/results/B4_results.txt
-bash: /krona/B7_results.txt.krona: No such file or directory
Drains_6/centrifuge/results/B7_results.txt
-bash: /krona/C10_results.txt.krona: No such file or directory
Drains_6/centrifuge/results/C10_results.txt
-bash: /krona/C1_results.txt.krona: No such file or directory
Drains_6/centrifuge/results/C1_results.txt
-bash: /krona/D7_results.txt.krona: No such file or directory
Drains_6/centrifuge/results/D7_results.txt
-bash: /krona/NC_D6_results.txt.krona: No such file or directory
Drains_6/centrifuge/results/NC_D6_results.txt
-bash: /krona/A6_results.txt.krona: No such file or directory
Drains_7/centrifuge/results/A6_results.txt
-bash: /krona/A8_results.txt.krona: No such file or directory
Drains_7/centrifuge/results/A8_results.txt
-bash: /krona/C7_results.txt.krona: No such file or directory
Drains_7/centrifuge/results/C7_results.txt
-bash: /krona/C9_results.txt.krona: No such file or directory
Drains_7/centrifuge/results/C9_results.txt
-bash: /krona/G6_results.txt.krona: No such file or directory
Drains_7/centrifuge/results/G6_results.txt
-bash: /krona/H1_results.txt.krona: No such file or directory
Drains_7/centrifuge/results/H1_results.txt
-bash: /krona/NC_D7_results.txt.krona: No such file or directory
Drains_7/centrifuge/results/NC_D7_results.txt

It is looping and showing me that at least its finding the files i want it to execute on but its still giving me the same error. Sorry for not being very useful in giving you more information to help.

ADD REPLY
0
Entering edit mode

Where do you want to save the results? If you want to save them in your current working directory, you could simply do this:

for k in */centrifuge/kreport/*.txt; do
 name=$(basename $k);
 cat $k | cut -f 1,3 > $name.krona;
 echo $k;
 done

If you want the result files in any other specific folder, then you could do something like this:

for k in */centrifuge/kreport/*.txt; do
 outputdir="/insert/here/your/path"; #Make sure that the path exists 
 name=$(basename $k);
 cat $k | cut -f 1,3 > $outputdir/$name.krona;
 echo $k;
 done

I hope this helps.

ADD REPLY

Login before adding your answer.

Traffic: 2301 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6