0

know question is not clear at a glance, i have this table:

   ID Start End
   1  1     4
   2  2     5
   3  4     9
   4  8     10

I want to set these in an order (illustration below). I need an array that its indices will increment by one with respect to start and the end positions, and get the greatest index of all. For example:

1. ####
2.  ####
3.    ######
4.        ### 

so array will be;
    array =(1,2,2,3,2,1,1,2,2,1)

i did not start to write anything because i could not figure whether that is possible with bash. please advice..

2
  • I am having trouble discerning the how the sequence 1,2,2,3,2,1,1,2,2,1 relates to your table of ranges. Commented Nov 30, 2012 at 14:23
  • @ddoxey those are just for display, i mean i want to fill them to have those values, so i that i can the greatest one.. Commented Nov 30, 2012 at 14:26

1 Answer 1

2

Just loop over all the elements of each interval:

#! /bin/bash

array=()
while read id start end ; do
    for (( i=start ; i<=end ; i++ )) ; do
        let array[i]++
    done
done << EOF
1  1     4
2  2     5
3  4     9
4  8     10
EOF

echo "${array[@]}"
Sign up to request clarification or add additional context in comments.

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.