Printing line in shell script

Need assistance in getting a shell program .
I have csv file and each line has comma separated number. I wanted to take of the comas and print each number in each line . below example. Appreicate your help

Row with number

KCAR,31,62,36,69,41,75,46,78,55,70,44,69,41,65,40,63,41,61,42,64,43,66,41,65,44,66,44,68,46,69

Need to be printed as

KCAR
max 31
min 62
avg 63
max 69
min 41
avg 75

and soo on

Please show us your attempts at solving this.

balajesuri

Below is the command I used and finding another way to shorten the command or script

awk 'BEGIN { FS = "," } ; { print $1"""\nmax "$2"\nmin "$3  }'
$ awk -F, '{print $1; for (i=2;i+2<=NF;i+=3) printf("max %d\nmin %d\navg %d\n", $i, $(i+1), $(i+2))}' input
KCAR
max 31
min 62
avg 36
max 69
min 41
avg 75
max 46
min 78
avg 55
max 70
min 44
avg 69
max 41
min 65
avg 40
max 63
min 41
avg 61
max 42
min 64
avg 43
max 66
min 41
avg 65
max 44
min 66
avg 44
max 68
min 46
avg 69
1 Like

Thank you neutronscott. Appreciate your help

Try

 awk -F, 'BEGIN {n=split ("max,min,avg", TMP)} {print $1; for (i=2; i<=NF; i++) print TMP[(i-2)%3+1], $i}' file

nice. :b: i fixed that for ya. :cool:

1 Like

Even nicer! And flexible ... I was close-minded on the 3.

neutronscott

Need one more modification need date to be added . Like the below example . Appreciate your help so much

KCAR
05/01/2015 max 31
05/01/2015 min 62
05/01/2015 avg 36
05/02/2015 max 69
05/02/2015 min 41
05/02/2015 avg 75

How about trying that one yourself?

Thanks RudiC for the advice . I am learning awk . It would be great if you can help me on this.

I don't see any dates in your input file??? Where do these dates that you want to add to the output come from?

Don Cragun forgot to mention. dates are not present.

Below data is 10 day data . example 1st day 31,62,36 which is max , min , avg then 2nd day max,min,avg. 3rd day max,min,avg as follows. So need to script it .

KCAR,31,62,36,69,41,75,46,78,55,70,44,69,41,65,40,63,41,61,42,64,43,66,41,65,44,66,44,68,46,69

That isn't the question. What determines the start date? Are all days data sequential? (Are weekends and holidays included in the data? What do you consider to be a holiday? ...)

If your input file is showing accumulated data that starts on 2015-05-01, how was the writer of this file able to predict minimum, maximum, and average data for a week in the future?

1 Like

Don Cragun : Thanks Don . Your previous solutions really helped me