How to insert/expand data/rows in a file, with rules??

Hi, I am rather new to Unix/Linus. I have this problem that I would like to solve using unix.

Here is what I have

start stop expression
1 5 15
2 6 10

I want a output like this

position expression
1 15
2 25
3 25
4 25
5 25
6 10

Can anyone help me withe this??

Thanks a lot

Hsiao.:slight_smile:

does this help...?

awk 'NR==1||/[0-9]+/{print $1,$3}' inputfile > outfile
awk '$2>m{m=$2}{A[$3]=$1;B[$3]=$2}END{for(i=1;i<=m;i++){t=0;for(j in A)if(i>=A[j]&&i<=B[j])t+=j;print i,t}}' infile

While reading the files, you need to store the minimum values and maximum values for which the amount applies.. I used arrays A and B for this. Also we need to determine the max value for which any rule applies. I used m for this. Then after the reading is over we have collected all the files and we can enumerate the values between 1 and max value and see what values should be added.

bash way: #!/bin/bash
while read A B C
do
for ((i=A; i<=B; i++)); do ((N[$i]+=C)); done
done <file
for i in ${!N[@]}; do echo "$i ${N[$i]}"; done