awk script not producing output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk script not producing output
# 1  
Old 04-15-2016
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):

Code:
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 script to produce this kind of output:

Code:
Color     Occurrence    Mean Value
=====    ============  ==========
Blue           2           4.5
Red            2           5.5
Yellow         1            4

So I created the below awk file which is not really optimized but I like to do it this way to understand what happens in every step.

Code:
#!/bin/bash
awk '
{
        if(!($1 in c))
        {
                colors[$1] = $1
        }
        TotalValue[$1] += $2
        occurrences[$1]++
}

END {   printf("%-11s %-12s %12s\n", "   Color   "," Occurrences ", " Mean Value ")
        printf("%-11s %-12s %12s\n", "===========", "============", "============")
        for(i in c)
                printf("%-11s %-12d %12d\n", colors[i], occurrences[i], TotalValue[i]/occurrences[i])
}' $1

I run the file with this command in Linux:

./awkFileFromAbove.sh inputFileWithColorsAndNumbersFromAbove.txt

The problem is that when I run this I don't get any output printed from the 'for loop'. Have tried to troubleshoot by putting printf() here and there, and I know the script enters the if statement for every single line (not once per color). Suggestions on how to proceed?

Last edited by Zooma; 04-15-2016 at 05:31 PM.. Reason: Corrected some typos (occurances -> occurrences in two places).
# 2  
Old 04-15-2016
Quote:
Originally Posted by Zooma
Suggestions on how to proceed?
Actually one: put "\n" at the end of your printf()s, because linefeeds do not come automatically. ;-))

I hope this helps.

bakunin
This User Gave Thanks to bakunin For This Post:
# 3  
Old 04-15-2016
- the array c is not populated; I guess you want to use colors
- occurrences is misspelled/inconsistently spelled in different places
- the %12d format can't print decimals for mean values

Try
Code:
awk '
{
        if(!($1 in colors))
        {
                colors[$1] = $1
        }
        TotalValue[$1] += $2
        occurences[$1]++
}

END {   printf("%-11s %-12s %12s\n", "   Color   "," Occurrences ", " Mean Value ")
        printf("%-11s %-12s %12s\n", "===========", "============", "============")
        for(i in colors)
                printf("%-11s %-12d %12f\n", colors[i], occurences[i], TotalValue[i] / occurences[i])
}' file
   Color     Occurrences   Mean Value 
=========== ============ ============
Blue        2                4.500000
Yellow      1                4.000000
Red         2                5.500000

This User Gave Thanks to RudiC For This Post:
# 4  
Old 04-15-2016
Hello Zooma,

You could try following too and let us know how it goes for you.
Code:
awk 'BEGIN{print "Color     Occurrence    Mean Value" ORS "=====    ============  =========="} FNR==NR{B[$1]+=$NF;C[$1]++;next} ($1 in B){print $1 OFS C[$1] OFS B[$1]/C[$1];delete B[$1];delete C[$1]}' Input_file OFS="\t\t" Input_file

Output will be as follows.
Code:
Color     Occurrence    Mean Value
=====    ============  ==========
Blue            2               4.5
Red             2               5.5
Yellow          1               4

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 5  
Old 04-15-2016
Thanks a lot for all suggestions, very useful information. I got the original file to work by changing the loop to:

Code:
for(i in colors)
                printf("%-17s %10d %14d\n", colors[i], occurrences[i], totalValue[i]/occurrences[i])

Sorry for some typos in the original question. They were not there in my file (I changed to colors to make the example look more understandable) and then I accidently did some typos. Will edit this in the question. Thanks for spotting.
# 6  
Old 04-15-2016
Note that your script:
Code:
#!/bin/bash
awk '
{
        if(!($1 in c))
        {
                colors[$1] = $1
        }
        TotalValue[$1] += $2
        occurrences[$1]++
}

END {   printf("%-11s %-12s %12s\n", "   Color   "," Occurrences ", " Mean Value ")
        printf("%-11s %-12s %12s\n", "===========", "============", "============")
        for(i in c)
                printf("%-11s %-12d %12d\n", colors[i], occurrences[i], TotalValue[i]/occurrences[i])
}' $1

doesn't really need the colors[] array. And, as RudiC noted, %d won't print a decimal point and fractional values that you said you want in your output. To get 1 digit after the decimal point in your averages (as in the output you said you wanted) you might want to try this slight modification to your original script:
Code:
#!/bin/bash
awk '
{
        TotalValue[$1] += $2
        occurrences[$1]++
}

END {   printf("%-11s %s %s\n", "   Color", "Occurrences", "Mean Value")
        printf("%s %s %s\n", "===========", "===========", "==========")
        for(i in TotalValue)
                printf("%-11s %11d %10.1f\n", i, occurrences[i], TotalValue[i]/occurrences[i])
}' "$1"

which, when invoked with one operand naming a file containing the sample data you provided in post #1 produces the output:
Code:
   Color    Occurrences Mean Value
=========== =========== ==========
Blue                  2        4.5
Red                   2        5.5
Yellow                1        4.0

This User Gave Thanks to Don Cragun For This Post:
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 Beginners Questions & Answers

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: 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... (10 Replies)
Discussion started by: Marquez3105
10 Replies

3. Shell Programming and Scripting

awk producing too many fields

Hey guys, awk is putting out too many results, two of each specifically. See below. Code: read CPU_VENDOR_ID <<< $(cat /proc/cpuinfo | grep "vendor_id" | awk -F: '{print $2}') echo $CPU_VENDOR_ID echo Output: AuthenticAMD AuthenticAMD I only want it to print out "AuthenticAMD" once. ... (7 Replies)
Discussion started by: 3therk1ll
7 Replies

4. 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

5. 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

6. Shell Programming and Scripting

Help, while loop and sed not producing desired output

Hello everyone, I need some assistance with what I thought would have been a very simple script. Purpose of Script: Script will parse through a source file and modify (search/replace) certain patterns and output to stdout or a file. Script will utilize a "control file" which will contain... (12 Replies)
Discussion started by: packetjockey
12 Replies

7. Shell Programming and Scripting

Script not working...producing 0's and not number

Below is my script: #!/bin/sh #echo "Please type oracle-lower case please:" #read X #if ] #then # echo "Sorry that is not oracle, try again" # exit 1 #else # echo Thank you #fi find / -name oracle 2>/dev/null | while read line do bdf 2>/dev/null |... (6 Replies)
Discussion started by: bigben1220
6 Replies

8. UNIX for Dummies Questions & Answers

Script producing error, Program to calculate maximum number

Hi folks, Here i have written a shell script to calculate a maximum number from 10 numbers entered on command line. max=0 echo Enter 10 numbers , one at a time for i in 1 2 3 4 5 6 7 8 9 10 do read n max=`expr $max + $n` if --- At this last step there is some problem, it gives error... (5 Replies)
Discussion started by: rits
5 Replies

9. Shell Programming and Scripting

sed in while loop producing arithmetic output

Hi all Just wondering if someone can help me with this. I'm trying to write a script that processes the output of another file and prints only the lines I want from it. This is only the second script I have written so please bare with me here. I have referred to the literature and some of the... (3 Replies)
Discussion started by: javathecat
3 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