Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

feed awk positional parameters

Tags:

bash

awk

Can someone please help me with a script that takes a list of integers and returns a response with the average, total, maximum and minimum values?

I need to be able to pass parameters when I run the script e.g:

./script.sh 1 2 3 4 5

I have an awk script that does the average, total and maximum sorted, I just need to add a bit so that it will read the arguments - so when I run ./script.sh 1 2 3 4 it returns:

Average: 3
Min: 1
Max: 5
Total: 16

Any help would be appreciated.

This is what I have already:

#!/bin/bash

awk 'NR == 1 { max=$1; min=$1; sum=0 }
        { if ($1>max) max=$1; if ($1<min) min=$1; sum+=$1;}
        END {printf "Min: %d\nMax: %d\nAverage: %d\nSum %0.0f\n", min, max, sum/NR, sum}'

Thanks in advance!

like image 544
gilgamesh2700 Avatar asked Jun 15 '26 02:06

gilgamesh2700


1 Answers

Like this?

printf '%s\n' "$@" | awk ...
like image 167
oguz ismail Avatar answered Jun 18 '26 01:06

oguz ismail



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!