linux nested for loops HELP ..
linux nested for loops HELP ..
(OP)
Hello all,
Need some help here ..
country=(argentina brazil china)
for (( i=0; i < ${#country[i]}; i++ )); do
for init in $(cat ~/files/zones.txt); do
echo ${country[i]} `cat ~/lists/$init.zone| wc -l`
done
done
#desired output
argentina 2000
brazil 400
china 5000
#wrong output I keep getting
argentina 2000
argentina 400
argentina 5000
brazil 2000
brazil 400
brazil 5000
china 2000
china 400
china 5000
Need some help here ..
country=(argentina brazil china)
for (( i=0; i < ${#country[i]}; i++ )); do
for init in $(cat ~/files/zones.txt); do
echo ${country[i]} `cat ~/lists/$init.zone| wc -l`
done
done
#desired output
argentina 2000
brazil 400
china 5000
#wrong output I keep getting
argentina 2000
argentina 400
argentina 5000
brazil 2000
brazil 400
brazil 5000
china 2000
china 400
china 5000
RE: linux nested for loops HELP ..
Hard to got the point there. Could you show us the content of ~/files/zones.txt ?
Feherke.
feherke.github.io
RE: linux nested for loops HELP ..
But as feherke says, what's in zones.txt?
RE: linux nested for loops HELP ..
ar
br
cz
# below are 3 the $init.zone files which are just IPs contents
# ar.zone
27.2.0.0/15
27.64.0.0/12
27.118.16.0/20
# br.zone
45.164.228.0/22
45.165.36.0/22
45.165.44.0/22
45.165.112.0/22
# cn.zone
27.126.152.0/22
36.255.60.0/22
36.255.106.0/23
36.255.244.0/22
RE: linux nested for loops HELP ..
Well, then you can not use cat as that will read the entire file each time.
What you want is to iterate over the array and the file content in parallel, so must use a single loop :
CODE --> Bash
Alternatively you could read the entire zones.txt into another array, then use the same loop control variable to get its matching item :
CODE --> Bash
Feherke.
feherke.github.io
RE: linux nested for loops HELP ..
argentina 1701
brazil 10396
china 8444
egypt 171
india 6002
iran 1482
italy 2903
korea 2020
philippines 594
russia 10497
turkey 1284
ukraine 3192
vietnam 706
romania 3399
thailand 676
germany 9295
sudan 31
syria 219
RE: linux nested for loops HELP ..
Oops. I missed that. Needs a small fix to compare with the array's length, not the current array item's length :
CODE --> Bash ( fragment )
Feherke.
feherke.github.io
RE: linux nested for loops HELP ..