# How do add values in a vector using a sliding window?

**URL:** <https://community.unix.com/t/how-do-add-values-in-a-vector-using-a-sliding-window/335779>\
**Category:** Shell Programming and Scripting\
**Created:** [October 22, 2013, 5:05pm UTC](https://community.unix.com/t/how-do-add-values-in-a-vector-using-a-sliding-window/335779 "2013-10-22T17:05:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Twinklefingers](https://community.unix.com/letter_avatar/twinklefingers/32/6_22734026db12803ebb3e059622ac3534.png) [@Twinklefingers](https://community.unix.com/u/Twinklefingers)\
**Post date:** [October 22, 2013, 5:05pm UTC](https://community.unix.com/t/how-do-add-values-in-a-vector-using-a-sliding-window/335779/1 "2013-10-22T17:05:34Z")

</div>

Greetings.

I have a vector of numbers such as the following:

```nohighlight
1
75
79
90
91
92
109
120
167
198
203
204
206
224
230
236
240
245
263
344
427
451
461
476
528
619
627
649
681
701
824
852
911
924
965
978
986

```

And I would like to know the number of values that fall within a certain sliding window, say 50. The result for the vector above, for a window-size of 50 is:

```nohighlight
50	1
100	5
150	2
200	2
250	8
300	1
350	1
400	0
450	1
500	2
550	1
600	0
650	3
700	1
750	1
800	0
850	1
900	1
950	2
1000	3

```

Showing that there is 1 entry falling in the range 1-50, 5 entries in 51-100, 2 entries for 101-150, etc.

Whereas, for a window-size of 250 the results should look like:

```nohighlight
250	18
500	6
750	6
100	7

```

I feel like there must be an easy one-liner for this, but I haven't been able to figure it out.

Thanks!

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [October 22, 2013, 5:24pm UTC](https://community.unix.com/t/how-do-add-values-in-a-vector-using-a-sliding-window/335779/2 "2013-10-22T17:24:50Z")

</div>

`awk -f twink.awk myFile`  
`awk -v w=250 -f twink.awk myFile`

twink.awk:

```nohighlight
BEGIN {
  if (!w) w=50
}
{ a[int($0/w)]++ }
END {
  for (i in a)
    print (i*w)+w, a
}

```

---

<div class="post-metadata">

**Author:** ![Chubler\_XL](https://community.unix.com/user_avatar/community.unix.com/chubler_xl/32/2077_2.png) [@Chubler\_XL](https://community.unix.com/u/Chubler_XL)\
**Post date:** [October 22, 2013, 5:33pm UTC](https://community.unix.com/t/how-do-add-values-in-a-vector-using-a-sliding-window/335779/3 "2013-10-22T17:33:27Z")

</div>

Do you require a zero count when the range is empty?

---

<div class="post-metadata">

**Author:** ![Twinklefingers](https://community.unix.com/letter_avatar/twinklefingers/32/6_22734026db12803ebb3e059622ac3534.png) [@Twinklefingers](https://community.unix.com/u/Twinklefingers)\
**Post date:** [October 22, 2013, 5:40pm UTC](https://community.unix.com/t/how-do-add-values-in-a-vector-using-a-sliding-window/335779/4 "2013-10-22T17:40:57Z")

</div>

@Chubler_XL Yes, please.

---

<div class="post-metadata">

**Author:** ![Chubler\_XL](https://community.unix.com/user_avatar/community.unix.com/chubler_xl/32/2077_2.png) [@Chubler\_XL](https://community.unix.com/u/Chubler_XL)\
**Post date:** [October 22, 2013, 5:42pm UTC](https://community.unix.com/t/how-do-add-values-in-a-vector-using-a-sliding-window/335779/5 "2013-10-22T17:42:24Z")

</div>

Try this slight change to vgersh99's post:

```nohighlight
awk -v w=250 '
!w{w=50}
{
 v=int($0/w)
 a[v]++
 m=(m>v)?m:v
}
END{for(i=0;i<=m;i++) print w+i*w, 0+a}' infile

```
