Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use for loop with two variable

I have a question about for loop in Bash I want to run awk command for specific range increasing by 34 but I don't know how to specify two variable in a for loop. I know how to do it for one variable but it is not working for two. this is my code for one variable:

#!/bin/bash
for a in {1..3400..34}
do
printf "awk 'NR>=$a&&NR<=$b { if (/^[0-9]/) sum++} END {print "row\t", sum }' file "
done

but I want to specify both variables ($a,$b), something like this which is not working! :

for a in {1..3400..34} , for b in {35..3400..34}
do
printf "awk 'NR>=$a&&NR<=$b { if (/^[0-9]/) sum++} END {print "row\t", sum }' hydr_dE.txt && "
done

Thanks,

like image 972
user3576287 Avatar asked May 05 '14 12:05

user3576287


1 Answers

Using C-style for loop:

for ((a=1,b=35;a<=3400,b<=3400;a+=34,b+=34)); do
    echo ": $a :: $b :"
done

(will have the same output as devnull's answer, but in pure Bash).

Of course, in this simple case, it's enough to just do:

for ((a=1;b=a+34,b<=3400;a+=34)); do
    echo ": $a :: $b :"
done

I'm sure you'll be able to figure out how to adapt this to what you exactly want.

like image 117
gniourf_gniourf Avatar answered Oct 03 '22 07:10

gniourf_gniourf