Nest 2 for loops in a single command line
1
1
Entering edit mode
2.2 years ago
caro-ca ▴ 20

Hi, community! I want to transform BED to VCF files, but I will use echo to simplify the example. For that, I have two folders to access my BED files.

directory A:              /home/caroca/strains/
folders:                  SRR800856  SRR800857  SRR800858

directory  B:             /home/caroca/strains/strain/results/
files for 1 folder:       SRR800856_ngs_nonredundant.bed SRR800856_rel_nonredundant.bed

This is my code:

for i in SRR*; do 
    cd  /home/caroca/strains/$i/results ; 
    for file in  /home/caroca/strains/$i/results/$i_*_.nonredundant.bed ; do 
        echo $file ;
    done ;
done

This is the stdout:

/home/caroca/strains/SRR800856/results/*_.nonredundant.bed
/home/caroca/strains/SRR800857/results/*_.nonredundant.bed
/home/caroca/strains/SRR800858/results/*_.nonredundant.bed

As you can see, the "*" should not stay there, instead it should be (the files of each folder):

/home/caroca/strains/SRR800856/results/SRR800856_ngs_nonredundant.bed
/home/caroca/strains/SRR800856/results/SRR800856_rel_nonredundant.bed

I tried multiple combinations and I don't know how to tackle it. Thank you in advance!

bash for loops • 768 views
ADD COMMENT
0
Entering edit mode

Hi,

Not sure if the error is related, but can you try this:

for i in SRR*; do cd  /home/caroca/strains/$i/results ; for file in  /home/caroca/strains/$i/results/${i}_*_.nonredundant.bed ; do echo $file ; done; done

I think you need to add {} because _ might be a special character: ${i}.

António

ADD REPLY
0
Entering edit mode

Thank you for your reply. Although this is what I got:

 /home/caroca/strains/SRR800854/results/SRR800854_*_.nonredundant.bed
  /home/caroca/strains/SRR800855/results/SRR800855_*_.nonredundant.bed
  /home/caroca/strains/SRR800856/results/SRR800856_*_.nonredundant.bed
  
ADD REPLY
0
Entering edit mode

I think you need to spend some time understanding bash wildcards and parameter expansion.

I would also suggest using find with its -exec flag for this instead.

ADD REPLY
0
Entering edit mode

Thank you! I don't have a lot of experience with bash but I will pay a close look at that. Thanks

ADD REPLY
2
Entering edit mode
2.2 years ago
2nelly ▴ 310

What about this:

for i in /home/caroca/strains/SRR*; do for f in $i/results/*nonredundant.bed; do echo $f; done; done

also you can have the same result in one loop:

for f in /home/caroca/strains/SRR*/results/*nonredundant.bed; do echo $f; done;
ADD COMMENT
0
Entering edit mode

Thank you so much for your help!! It was really clever!! I don't have a lot of experience with bash, but I am on it.
Thanks!

ADD REPLY
0
Entering edit mode
$ for f in /home/caroca/strains/SRR*/results/*nonredundant.bed; do echo $f; done;

is this equivalent to

$ find  /home/caroca/strains/SRR* -type f -name "*nonredundant.bed" ?
ADD REPLY
0
Entering edit mode

yes, but only for printing the name files. Caro-ca generalized the example. In fact, he wants to apply a conversion from bed to vcf. Thence, he will replace echo with something else.

Of course he can use find command and then xargs to do the same thing. But maybe it is better for him to have one step at a time

ADD REPLY

Login before adding your answer.

Traffic: 1370 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