简体   繁体   中英

Bash- create an array and increment each index- then the greatest index

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..

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[@]}"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM