5

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,

6
  • so what you wanted is just printing the awk codes with a, b expanded? or you want the awk codes to get executed? Commented May 5, 2014 at 12:13
  • My final aim is to execute the awk code for range between 1-3400 with that range which I couldn't find any solution. So I tried to print it out and run the output by using && in middle of each awk! but my first question is about adding two variable in a for loop. I am not sure if I should open another question or not? Commented May 5, 2014 at 12:15
  • can you make an example to explain what you want? very likely you were on a wrong way. you could use smaller a b and step to make the question simpler. Commented May 5, 2014 at 12:18
  • Sure! I have a file with 3400 rows. I want my script to: first divide the rows into 100 parts from 1-34 , 35-68 and so on. Then count the number of rows which contain numbers not character like in my awk { if (/^[0-9]/) sum++} and give me a tab delimited outputs from divided rows (1-100) and the counted numbers. Sorry for my bad English! Commented May 5, 2014 at 12:26
  • when I mentioned "examples" it means your input files expected outputs... of course, the simplified version from your real data. Commented May 5, 2014 at 12:27

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

Comments

0

(1) If you need to loop over two variables, you'd probably want to ensure that those contain the same amount of data. echo {1..3400..34} would produce one value more than echo {35..3400..34}.

(2) Assuming that the two inputs are of the same size, you could use a while loop:

while read a b; do
  echo ": $a :: $b :";
  # do something with a and b here
done < <(paste <(seq 1 34 3400) <(seq 35 34 3434))

(You could use the loop with the two inputs of unequal size too, but that is probably what you don't want. In the above example, the variable a would consume the longer input.)

The <(command) form is referred to as process substitution.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.