In the following I've put up my code for iterating over lines of a file in order to collect data within a segment from the file in following fashion:
Illustration for understanding procedure:
segL segH
| |
[ 2 4 9 15 25 45 ... 99 ] 102 136 ... 206
Later, for a file with 10000 lines I'd like to divide the file into segments of equal length with a lower boundary of segL
and segH=segL+segsize
.
Walking through the file, I'd like to count the number of integers meeting following condition:
segL < integer =< segH
.
This number then should be stored in a variable uniquely containing the amount of integers for that very segment only!
Code
segsize=100
segL=0
segH=100
blockcounter=0
segment1=0
segments2=0
#Go through input and partition it
for i in {1..2}
do
while read p; do
if [ $p -gt $segL ] && [ $p -le $segH ]
then
blockcounter=$(($blockcounter + 1))
fi
done <$inputfile
if [ "$i" -eq "1" ]
then
segment1=$blockcounter
echo "segment1: $segment1"
fi
if [ "$i" -eq "2" ]
then
segment2=$blockcounter
echo "segment2: $segment2"
fi
blockcounter=0
segL=$segH
segH=$(($segL + $segsize))
done
Right now, in my code I'm able to collect data for 2 segments only - with my file, currently, containing a bit more than 200 integers (2*segments).
Output:
segment1: 27
segment2: 33
For larger samples I need to store data into up to 100-120 segments with a comparable output mentioned above.
Can you think of alternatives for achieving same output (2D-array like object for storing data points, e.g. A(segment1|<count of integers>)
?
declare -A array
thenarray[seg${i}_int$j]=$value
or something along those lines.