Sponsored Content
Top Forums Shell Programming and Scripting awk script not producing output Post 302971089 by Zooma on Friday 15th of April 2016 01:44:31 PM
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).
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
All times are GMT -4. The time now is 01:27 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy