Question: Nest 2 for loops in a single command line
1
gravatar for caro-ca
6 weeks ago by
caro-ca20
caro-ca20 wrote:

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 • 154 views
ADD COMMENTlink modified 6 weeks ago by 2nelly210 • written 6 weeks ago by caro-ca20

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 REPLYlink written 6 weeks ago by antonioggsousa1.3k

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 REPLYlink modified 6 weeks ago • written 6 weeks ago by caro-ca20

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 REPLYlink written 6 weeks ago by Joe17k

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

ADD REPLYlink written 6 weeks ago by caro-ca20
2
gravatar for 2nelly
6 weeks ago by
2nelly210
Geneva,Switzerland
2nelly210 wrote:

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 COMMENTlink modified 6 weeks ago • written 6 weeks ago by 2nelly210

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 REPLYlink written 6 weeks ago by caro-ca20
$ 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 REPLYlink modified 6 weeks ago • written 6 weeks ago by cpad011214k

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 REPLYlink modified 6 weeks ago • written 6 weeks ago by 2nelly210

I hope you could help me out with this new post. Thank you.

ADD REPLYlink modified 6 weeks ago • written 6 weeks ago by caro-ca20
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 709 users visited in the last hour