Using awk to create a summary of a structured file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using awk to create a summary of a structured file
# 1  
Old 09-18-2012
Using awk to create a summary of a structured file

I am trying to use awk to create a summary of a structured file. Here is what it looks like:


Code:
(random text)

H1   H2   H3   H4
44   78   99   30
31   --   32   21
12   33   55   21

I'd like to be able to specify a column, say H2, and then have information about that column printed. The information I am interested in is the number of entries under H2 (ignoring any -- entries) and then the min/max and finally the average. I am completely new to awk and so any help would be appreciated.
# 2  
Old 09-18-2012
Is "H2" unique within the file (ie can we guarantee it will not appear in the random text).

If not can we say the last occurance of H2 within the file will be the heading line?
# 3  
Old 09-18-2012
The last occurrence of H2 will be the heading line.
# 4  
Old 09-18-2012
I think this will work. Column number and the header text are supplied as variables so it should be flexible.

Code:
awk '
    $(col) == header {   #reset things each time we see the desired header
        v = 0;
        sum = 0;
        min = 9e9;
        max = -9e9;
        next;
    }
    $(col) != "--" {  
        v++;
        sum += $(col);
        if( $(col) > max )
            max = $(col);
        if( $(col) < min )
            min = $(col);
    }
    END {
        printf( "values=%d  min=%d  max=%d  mean=%d\n", v, min, max, sum/v );
    }
' col=2 header="H2"   input-file  >output-file

# 5  
Old 09-18-2012
Thank you.
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to create a summary file of all files in a directory sorted in reverse alphabetical order.?

I have an interactive script which works terrific at processing a folder of unsorted files into new directories. I am wondering how I could modify my script so that( upon execution) it provides an additional labelled summary file on my desktop that lists all of the files in each directory that... (4 Replies)
Discussion started by: Braveheart
4 Replies

2. Shell Programming and Scripting

Generating summary data (use awk?)

I have a data file similar to this (but many millions of lines long). You can assume that it is totally unsorted but has no duplicate rows. Date ,Tool_Type ,Tool_ID ,Time_Used 3/13/2014,Screwdriver,Screwdriver02, 6 3/13/2014,Screwdriver,Screwdriver02,20... (2 Replies)
Discussion started by: Michael Stora
2 Replies

3. Shell Programming and Scripting

Summary using awk

Displaying information using awk Hey guys, i am using awk to display my information in a certain order. Database : Persia:42:John France:50:Mabel Persia:50:Rach Germany:60:JohnMy expected output is : ... (25 Replies)
Discussion started by: gregarion
25 Replies

4. Shell Programming and Scripting

structured file update

Hi I have a very structured file consisting of multiple lines as follows: 3752 AVAILABLE 06/24/2009 FFFF 000000 0000 0000 3753 TRADITION (ASIA) LTD TACB 008329 0000 0000 3754 WACHOVIA CONVS/PRFDS WBCP 001099 0000 0000 3755 AVAILABLE 05/12/2009 FFFF 000000 0000 0000 3756... (3 Replies)
Discussion started by: aoussenko
3 Replies

5. Shell Programming and Scripting

Create Summary file containg information

Folks, I have multiple files in a folder containing some information (there is around 100 of them). What I would like to do would be able to import some of the information into a summary text file so that it will be easier to read a glance. The name of the files all start with the naming... (4 Replies)
Discussion started by: lodey
4 Replies
Login or Register to Ask a Question