user964689
user964689

Reputation: 822

Sort command not working as expected

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.

Upvotes: 0

Views: 230

Answers (2)

Shahbaz
Shahbaz

Reputation: 47563

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

Upvotes: 1

abhshkdz
abhshkdz

Reputation: 6365

To iterate over files in a folder,

for i in folder/*;
do <whateveryouwanttodo>;
done;

Hope this helps. :)

Upvotes: 1

Related Questions