Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8200923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:37:17+00:00 2026-06-07T06:37:17+00:00

If anybody can help me to write a loop to iterate over files in

  • 0

If anybody can help me to write a loop to iterate over files in a folder it would save me a huge amount of time. I think it must be quite a simple solution ,but I currently don’t know how to nest a loop within a loop. So far I have this script:

cd /folderlocation/
for i in `</textfile_containing_lines_to_iterate_through`
do
 #size=`echo $i | perl -nE '/:([\d-]+)/ && say abs(eval $1)'`
  #echo "$size"
 zcat dataset | head -n 18 > temp"$i".vcf
 tabix dataset $i >> temp"$i".vcf 
 vcftools --window-pi 1000000 --vcf temp10individuals"$i".vcf >>  run_summary.txt
cat out.windowed.pi >> outputfile_2 
  #rm temp*
done
grep -v "PI" outputfile_2   > outputfile
rm outputfile_2

I need to expand this so that the script will run multiple times, through all of the ‘textfiles_containing_lines_to_iterate_through’. Currently I change the name of the textfile manually each time and re-run the script. So I’d need a loop that does this for file in folder, and also that uses the name of the file as part of the outputfile name so that I can match an output file to an inputfile.

Any help would be really useful and greatly appreciated!

Many thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T06:37:18+00:00Added an answer on June 7, 2026 at 6:37 am

    You can nest loops the same way as any other language. You just write one inside the other.

    Let’s first look at how to loop through all files in the current directory:

    for file in *
    do
        do_something_with $file
        or_generate ${file}_changed
    done
    

    Now your whole script becomes:

    cd /folderlocation/
    for f in *
    do
      for i in `<$f`
      do
        #size=`echo $i | perl -nE '/:([\d-]+)/ && say abs(eval $1)'`
        #echo "$size"
        zcat dataset | head -n 18 > temp${i}.vcf
        tabix dataset $i >> temp${i}.vcf 
        vcftools --window-pi 1000000 --vcf temp10individuals${i}.vcf >>  run_summary.txt
        cat out.windowed.pi >> outputfile_2 
        #rm temp*
      done
      grep -v "PI" outputfile_2   > output_${file}
      rm outputfile_2
    done
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anybody help me out? I would like to store a hex colorPicker value
I got the exception to create. Can anybody help me? How to write nested
Can anybody help me out, I'm stuck, don't know how to write a php
I was wondering if anybody can help me to write a javascript code that
Can anybody help me to write a regex expression which accpets - Numbers +
Can anybody help me how can I invoke a JSP page from a Servlet
can anybody help me understand how Honda achieved this effect: http://testdays.hondamoto.ch/ I mean the
Can anybody help me in finding out how to verify that _tmkdir succeeded. For
Can anybody help how to setup a automatic backup of mysql database and then
can anybody help me how to nice integrate doctrine 2.0 into ZF 1.10? I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.