awk Script Output Help


 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers awk Script Output Help
# 1  
Old 02-21-2019
awk Script Output Help

My code is listed below, I'm trying to figure out what the problem is and what I can do to fix it. The output i'm getting is:



Code:
Name Low High Average
 0 0 0.00



The correct output I want is the name of the Assignment, the lowest score and highest score obtained, and the Average Score under each column. Can Someone help and point me in the right direction



Code:
 awk 'BEGIN{

FS = " "

print "Name Low High Average"

i=0

}

{

if(i==0){

i+=1

}

else{

if(low[$3]<$4)

low[$3] = $4

if(high[$3]>$4 || high[$3]==0)

high[$3] = $4

avg[$3] += $4

cnt[$3] += 1

}

}

END{

for(k in low){

avg[k] = avg[k]/cnt[k]

printf "%s %d %d %.2f ",k, low[k], high[k], avg[k]

}

}' ./Lab03-data.csv


Last edited by RudiC; 02-21-2019 at 06:05 PM..
# 2  
Old 02-21-2019
Hmmm...
Just a guess (without seeing your sample file)....
you're processing a .csv file, but you're setting your FS=" " ?
# 3  
Old 02-21-2019
Sample File

Heres the sample file:
should i use -f or -F in replace of FS?
Code:
Student Catehory  Assignment  Score  Possible    Chelsey  Homework  H01  90  100    Chelsey  Homework  H02  89  100    Chelsey  Lab  L01  91  100    Chelsey  Lab  L02  100  100    Chelsey  Quiz  Q01  100  100    Chelsey  Quiz  Q02  100  100    Chelsey  Final  FINAL  82  100    Sam  Homework  H06  97  100    Sam  Homework  H07  52  100    Sam  Lab  L06  0  100    Sam  Lab  L07  0  100    Sam  Quiz  Q06  95  100    Sam  Quiz  Q07  68  100    Sam  Final  FINAL  58  100    Andrew  Homework  H06  58  100    Andrew  Homework  H07  52  100    Andrew  Lab  L01  87  100    Andrew  Lab  L02  45  100    Andrew  Quiz  Q06  94  100    Andrew  Quiz  Q07  58  100    Andrew  Final  FINAL  99  100    Ava  Homework  H06  84  100    Ava  Homework  H07  55  100    Ava  Lab  L01  66  100    Ava  Lab  L02  77  100    Ava  Quiz  Q06  77  100    Ava  Quiz  Q07  88  100    Ava  Final  FINAL  99  100    Shane  Homework  H06  80  100    Shane  Homework  H07  90  100    Shane  Lab  L01  90  100    Shane  Lab  L02  0  100    Shane  Quiz  Q05  80  100    Shane  Quiz  Q06  80  100    Shane  Final  FINAL  90  100


Last edited by vgersh99; 02-21-2019 at 05:07 PM.. Reason: Code tags, please!
# 4  
Old 02-21-2019
Maybe try massaging the file before the awk. Using the sample posted:
Code:
tr " " '
' < sample_file | awk '
NR<6 {next}
{printf $0 FS}
!(NR % 5) {print _}
' | awk ...(your awk)...

# 5  
Old 02-21-2019
So just to be clear, I insert the coding you provided in front of mine and insert my coding where it says your awk correct?
# 6  
Old 02-21-2019
I doubt your sample file is what you actually have.
Please check the original sample from post #3 edited by me using the code tags.
If it's not what you actually have repost using the code tags.
# 7  
Old 02-21-2019
To cast your file into single line records, try

Code:
$ paste -sd"\t\t\t\t\n" <(tr -s ' ' $'\n' <file)
Student    Catehory    Assignment    Score    Possible
Chelsey    Homework    H01    90    100
Chelsey    Homework    H02    89    100
Chelsey    Lab    L01    91    100
Chelsey    Lab    L02    100    100
.
.
 .

EDIT: How far would this get you:

Code:
paste -sd"\t\t\t\t\n" <(tr -s ' ' $'\n' <file) | awk '
BEGIN           {print "Name\tLow\tHigh\tAverage"
                }
NR == 1         {next
                }
!($1 in MIN)    {MIN[$1] = 1E100
                }
                {if (MIN[$1]>$4) MIN[$1] = $4
                 if (MAX[$1]<$4) MAX[$1] = $4
                 SUM[$1] += $4
                 CNT[$1]++
                }
END             {for (m in MAX) print m, MIN[m], MAX[m], SUM[m]/CNT[m]
                }
' OFS="\t"
Name    Low    High    Average
Sam	0	97	52.8571
Chelsey	82	100	93.1429
Andrew	45	99	70.4286
Shane	0	90	72.8571
Ava	55	99	78


Last edited by RudiC; 02-22-2019 at 05:45 AM.. Reason: Corrected small error
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Shell script to call and sort awk script and output

I'm trying to create a shell script that takes a awk script that I wrote and a filename as an argument. I was able to get that done but I'm having trouble figuring out how to keep the header of the output at the top but sort the rest of the rows alphabetically. This is what I have now but it is... (1 Reply)
Discussion started by: Eric7giants
1 Replies

2. UNIX for Dummies Questions & Answers

Combine Both Output from the awk Script

Hi, Is there anyway to combine output from the awk scripting. file01.txt: AUE_CHMOD AUE_CHOWN AUE_CHROOT AUE_CONNECT AUE_ACCEPT AUE_FCHOWN AUE_FCHMOD AUE_SETREUID AUE_SETREGID AUE_FCHROOT AUE_PFEXEC AUE_SETUID AUE_NICE AUE_SETGID (9 Replies)
Discussion started by: alvinoo
9 Replies

3. Shell Programming and Scripting

awk script not producing output

Hi, I have a text file with some thousands of rows of the following kind (this will be referred to as the inputFileWithColorsAndNumbers.txt): Blue 6 Red 4 Blue 3 Yellow 4 Red 7 Colors in the left column and a number in the right one for each line. I want to run an awk... (5 Replies)
Discussion started by: Zooma
5 Replies

4. Shell Programming and Scripting

awk Script to format output

Hi all, I'm fairly new to this and learning along the way, so bare with me... I'm trying to format the output from a script to be more read-friendly. The output contains more servers and more processes but for as example it will do: Server: Test1 ... (4 Replies)
Discussion started by: Bobsonm
4 Replies

5. Shell Programming and Scripting

awk script to search output for a value and print

GOODNUMBERS="1 2 3 4 5 6 3 3 34 34 5 66 12" BADNUMBERS="7 3 12 5 66" for eachnum in `echo ${GOODNUMBERS}` do echo ${BADNUMBERS} | gawk -v threshold=${eachnum} '$1 != threshold' done what im trying to do with the above is, i want to print numbers that are in the GOODNUMBERS... (10 Replies)
Discussion started by: SkySmart
10 Replies

6. Shell Programming and Scripting

How to get fields and get output with awk or shell script.?

I have a flat file A.txt with field seperate by a pipe 2012/11/13 20:06:11 | 284:hawk pid=014268 opened Locations 12, 13, 14, 15 for /home/hawk_t112/t112/macteam/qt/NET12/full_ddr3_2X_FV_4BD_1.qt/dbFiles/t112.proto|2012/11/14 15:19:26 | still running |norway|norway 2012/11/14 12:53:51 | ... (6 Replies)
Discussion started by: sabercats
6 Replies

7. Shell Programming and Scripting

Awk script to run a sql and print the output to an output file

Hi All, I have around 900 Select Sql's which I would like to run in an awk script and print the output of those sql's in an txt file. Can you anyone pls let me know how do I do it and execute the awk script? Thanks. (4 Replies)
Discussion started by: adept
4 Replies

8. Shell Programming and Scripting

Colon in awk script output

I'm using AIX 5.3 and running a awk replace to modify data as follows: echo 1234: 1234 123 123 444 555 666 7777 | awk '/^:/{split($2,N);n=N} {n=$1} {sub(n,n+10000000)}1' 10001234 1234 123 123 444 555 666 7777 dumb question.. how do I get the colon back in, so it outputs 10001234: 1234... (4 Replies)
Discussion started by: say170
4 Replies

9. Shell Programming and Scripting

Format output using awk in script.

Guys, I have a script which hits the database and pulls the information that I need into files. Now I want to format these files to make them easy to read. The sample format of the file will be like.... <Start_of_File> Header1 .....xsdfsfa...adfa...... Header2 ....afefas .aefaefsdf...... (8 Replies)
Discussion started by: bperl
8 Replies

10. Shell Programming and Scripting

script the output with awk

Please i need your help. i made this script with awk, this scripts count and list a pattern for each directory in the output as shown. but not as desired. i want the output will be listed in a tabular way, using awk: cuenta_cdrs() { for dir in * do cd $dir for file in * do ... (4 Replies)
Discussion started by: alexcol
4 Replies
Login or Register to Ask a Question