Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please help with this AWK and UNIX code

Status
Not open for further replies.

JamesOwen

Programmer
Nov 8, 2011
12
GB
HI Guy's

Can I please get some help with this code.

I have xml feed file which rapidly changing temporary file and I need to capture the content of this file as soon as data arrives.

Example of the data

[date+time], message=[DATA= "<?xml version="1.0?"><data changeMsg><NAME="John Smith"><Age="23"><D.O.B="11-10-1988"> <Gender="Male">"
[date+time], message=[DATA= "<?xml version="1.0?"><data changeMsg><NAME="Emy Williams"><Age="23"><D.O.B="01-05-1988"> <Gender="Female">"
[date+time], message=[DATA= "<?xml version="1.0?"><data changeMsg><NAME="Jack Adam"><Age="66"><D.O.B="24-07-1945"> <Gender="Male">"
[date+time], message=[DATA= "<?xml version="1.0?"><data changeMsg><NAME="Charlie Daniel"><Age="38"><D.O.B="15-08-1973"> <Gender="Male">"
[date+time], message=[DATA= "<?xml version="1.0?"><data changeMsg><NAME="Ruby James"><Age="38"><D.O.B="11-03-1973"> <Gender="Female">"
[date+time], message=[DATA= "<?xml version="1.0?"><data changeMsg><NAME="Sophie Thomas"><Age="20"><D.O.B="12-09-1991"><Gender="Female">"

Required data output

8:30,Male,23,1
8:31,Female,23,1
8:32,Female,30,4
8:33,Male,50,10

Time is current time.

This is a ksh and awk code that I have so far but this doesn't do what I need it to do. Can I please get help with it.

All I want the code to do is to run for 2 minutes process the counts , write it to output then do the same process again and again.

code

awk 'BEGIN { INTERVAL=120;    "date +%s"|getline sec;
    NEXT=sec+120;}

    {
        if(sec >= NEXT)
        {
           printf( "\nSummary\n" );
           for( x in agcount )
              printf( "%s,%d\n", x, agcount[x] ) | "sort";

           NEXT=sec+120;
        }

        gsub( ">", "" );        # strip uneeded junk and make "foo bar" easy to capture
        gsub( " ", "~" );
        gsub( "<", " " );

        for( i = 1; i <= NF; i++ )          # snarf up each name=value pair
        {
            if( split( $(i), a, "=" ) == 2 )
            {
                gsub(  "\"", "", a[2] );
                gsub(  "~", " ", a[2] );
                values[a[1]] = a[2];
            }
        }

        #gcount[values["Gender"]]++;         # collect counts
        #acount[values["Age"]]++;
        agcount[values["Gender"]","values["Age"]]++;

        printf( "%s %s %s %s\n", values["NAME"], values["Age"], values["D.O.B"], values["Gender"] );
    }' input-file

I can't use gawk or cron scheduler.

Will anyone be able to help me with this?

any help would be greatly appreciated.

James
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top