Printing awk outputs

Hello All,

I have the following command which works partially:

gzcat *2016-03-25_*gz | gawk -F"|" '
BEGIN{format = "%-10s %-13s %-17s %-35s\n"; 
printf format, "EVENT_TYPE","RESPONSE_CODE","INTERNAL_ERR_CODE","FLOWNAME"; 
printf format, "----------", "-------------", "-----------------", "-----------------------------------"}
{if(( $5 == 2001 ) && ( $8 != 0 ) && ( $12 ~ "fulfillService" )) printf format, $5,$8,$9,$(NF-1)}' 
| sort | uniq -c

And its output is :

   1 ---------- ------------- ----------------- -----------------------------------
   2 2001       -2            100               refillVoucherless                  
   1 2001       -2            320001            deactivateBBService                
   1 2001       -3            -3                updateDataOfferSelectionRetrieve   
   2 2001       -3            104               refillVoucherless                  
5259 2001       -3            114               refillVoucherless                  
13482 2001       -3            115               refillVoucherless                  
   1 2001       -3            4020123           updateDataOfferSelection           
   1 2001       -4            1002              setHLRSub                          
  26 2001       -4            11111             addAndActivateService              
  12 2001       -4            13                setHLRSub                          
 267 2001       99            0                 updateDataOfferSelectionRetrieve   
   1 EVENT_TYPE RESPONSE_CODE INTERNAL_ERR_CODE FLOWNAME  

I guess "Sorting" after gawk command does mix things up and cause the output which I don't want. How could I print output like the following? Should I get rid of Sort and use something inside gawk? if so I would appreciate your help very much.

   1 EVENT_TYPE RESPONSE_CODE INTERNAL_ERR_CODE FLOWNAME  
   1 ---------- ------------- ----------------- -----------------------------------
   2 2001       -2            100               refillVoucherless                  
   1 2001       -2            320001            deactivateBBService   
   .  ..... .......... ...

uniq need its input to be sorted to achieve a reasonable result. If you want the header on top, print it to stdout, and the rest of the lines print to a pipe into sort | uniq .

You need to keep the headers from being sorted.

Untested, and taking a bit of artistic license with the headers, but this should come close to what you need:

format='%-10s %-13s %-17s %-35s\n'
printf " CNT $format" 'EVENT_TYPE' 'RESPONSE_CODE' 'INTERNAL_ERR_CODE' 'FLOWNAME'
printf "---- $format" '----------' '-------------' '-----------------' '-----------------------------------'
gzcat *2016-03-25_*gz | gawk -F"|" -v format="$format" '
{if(( $5 == 2001 ) && ( $8 != 0 ) && ( $12 ~ "fulfillService" ))
    printf format, $5,$8,$9,$(NF-1)}' | sort | uniq -c

Thanks for the suggestions mates, I have concluded the task liek the following:

echo -n "\n-----\t----------\t-------------\t\t--------------\t\t--------\nCOUNT\tEVENT_TYPE\tRESPONSE_CODE\t\tINTERNAL_ERROR\t\tFLOWNAME\n-----\t----------\t-------------\t\t--------------\t\t--------------------------------\n" && gzcat *2016-03-29_*gz | gawk -F"|" 'BEGIN{format = "%-10s\t%-8s\t%-14s\t%-32s\n"}{if(( $5 == 2001 ) && ( $8 != 0 ) && ( $12 ~ "fulfillService" )) printf format, "\t"$5"\t",$8"\t\t",$9"\t",$(NF-1)}' | sort | uniq -c && echo -n "-----\t----------\t-------------\t\t--------------\t\t--------------------------------\n"

Output:

-----   ----------      -------------           --------------          --------
COUNT   EVENT_TYPE      RESPONSE_CODE           INTERNAL_ERROR          FLOWNAME
-----   ----------      -------------           --------------          --------------------------------
2854    2001            -3                      114                     refillVoucherless               
7075    2001            -3                      115                     refillVoucherless               
   4    2001            -4                      11111                   addAndActivateService           
 121    2001            99                      0                       updateDataOfferSelectionRetrieve
-----   ----------      -------------           --------------          --------------------------------

IMO move the external pipeline inside of awk as doing it this way will sort on the data and leave the headers untouched...

gzcat *2016-03-25_*gz | gawk -F"|" '
BEGIN{format = "%-10s %-13s %-17s %-35s\n"; 
printf format, "EVENT_TYPE","RESPONSE_CODE","INTERNAL_ERR_CODE","FLOWNAME"; 
printf format, "----------", "-------------", "-----------------", "-----------------------------------"}
{if(( $5 == 2001 ) && ( $8 != 0 ) && ( $12 ~ "fulfillService" )) printf format, $5,$8,$9,$(NF-1) | "sort | uniq -c"}'

Probably due to EAGL�'s insistence on posting extremely long one-liners, you missed that there is also a trailer line being printed. To take care of that you need to add an END clause...

END{printf format, "----------", "-------------", "-----------------", "-----------------------------------"}

to your awk / gawk script.

1 Like

This thread looks very similar to this one, so pls. compare the solutions.