Search code examples
regexawkgawk

Match a pattern and print only non zero field using awk


I have a file like this, and I want to search for pattern matching "/4126/" and print only the month and year and the amount (the amount is not always in Jan 2014 as in example below).

awk -F! '/4126/ {print $0}'  prints the entire line

But I need to get it printed only the month/year and amount as follows :

Jan 2014
25492.00

A sample from the file is given here.

    +=====================================================================+
    ! Code  !  Jan 2014 !  Feb 2014 !  Mar 2014!    Arrears!  T o t a l s !
    +=====================================================================+
    ! 1101  !  26290.00 !  26290.00 !  26290.00!      0.00 !  3,15,480.00 !
    ! 1102  !    480.00 !    480.00 !    480.00!      0.00 !     5,760.00 !
    ! 2104  !  24213.09 !  25198.97 !  25198.97!      0.00 !  2,73,205.69 !
    ! 2107  !      0.00 !      0.00 !      0.00!      0.00 !    14,991.20 !
    ! 2113  !    275.00 !    275.00 !    275.00!      0.00 !     3,300.00 !
    ! 4114  !      0.00 !      0.00 !   1106.00!      0.00 !     4,424.00*!
    ! 4123  !   4667.00 !      0.00 !      0.00!      0.00 !     4,667.00 !
    ! 4126  !  25492.00 !      0.00 !      0.00!      0.00 !    25,492.00*!

Please provide me awk formula to do this. Thanks in advance.


Solution

  • You're almost there, $0 is the whole line, you need a specific field (and header)

    $ awk -F! 'NR==2{h=$3} $2~/\y4126\y/{print h; print $3}' file
    
    Jan 2014 
    25492.00 
    

    your sample output prints the previous value, if it's not a typo you should keep the previous line and print after a match.

    To eliminate false matches, keep the pattern to the corresponding field and with word boundaries.

    To print all nonzero amounts you can do the following

    $ awk -F! 'NR==2{h[3]=$3; h[4]=$4; h[5]=$5}
       $2~/\y2104\y/{for(i=3;i<=5;i++) 
                        if($i!=0) 
                           {header=header OFS h[i]; 
                            line=line OFS $i
                           } 
                    print header;
                    print line}' file 
    
    
       Jan 2014    Feb 2014    Mar 2014
       24213.09    25198.97    25198.97