Vertical total and listing

file 1

voucher    DOB           amount    name                              type
======   =======    =======  ==================   ===
37836452	1/13/1961	125000	ASHUTOSH	65
37836505	1/13/1961	500000	ASHUTOSH	49
37836455	1/13/1961	100000	ASHUTOSH	65
37836488	1/13/1961	250000	ASHUTOSH	75
37836473	1/13/1961	125000	ASHUTOSH	49
37836478	1/13/1961	250000	ASHUTOSH	49
37836519	1/13/1961	150000	ASHUTOSH	49
37836437	1/13/1961	500000	ASHUTOSH	49
37836461	1/13/1961	125000	ASHUTOSH	65
37836477	1/13/1961	250000	ASHUTOSH	65
37836486	1/13/1961	250000	ASHUTOSH	65
37836513	1/13/1961	500000	ASHUTOSH	65
37836431	1/13/1961	125000	ASHUTOSH	65
37836427	5/31/1991	350000	JOHNSON	75
37836470	5/31/1991	125000	JOHNSON	75
37836511	5/31/1991	62500	JOHNSON	75
37836520	5/31/1991	500000	JOHNSON	75
37836484	5/31/1991	200000	JOHNSON	75
37836435	5/31/1991	250000	JOHNSON	75
37836433	5/31/1991	150000	JOHNSON	65
37836482	5/31/1991	100000	JOHNSON	78
37836516	5/31/1991	100000	JOHNSON	65
37836497	5/31/1991	62500	JOHNSON	65
37836453	5/31/1991	375000	JOHNSON	75
37836506	7/12/1983	125000	LEENA	75
37836499	7/12/1983	125000	LEENA	75
37836507	7/12/1983	115000	LEENA	75
37836521	7/12/1983	250000	LEENA	65
37836465	7/12/1983	62500	LEENA	65
37836489	7/12/1983	500000	LEENA	49
37836459	7/12/1983	62500	LEENA	65
37836429	7/12/1983	250000	LEENA	65
37836436	7/12/1983	500000	LEENA	65
37836447	7/12/1983	500000	LEENA	65
37836476	8/12/1987	250000	MATHEWS 	65
37836469	8/12/1987	100000	MATHEWS 	75
37836454	8/12/1987	500000	MATHEWS 	75
37836460	8/12/1987	375000	MATHEWS 	75
37836522	8/12/1987	125000	MATHEWS 	65
37836467	8/12/1987	62500	MATHEWS 	65
37836446	8/12/1987	125000	MATHEWS 	49
37836430	8/12/1987	62500	MATHEWS 	49
37836449	8/12/1987	75000	MATHEWS 	49
37836475	8/12/1987	125000	MATHEWS 	49
37836474	8/12/1987	125000	MATHEWS 	49
37836457	8/12/1987	200000	MATHEWS 	89
37836466	8/12/1987	125000	MATHEWS 	65
37836451	8/12/1987	125000	MATHEWS 	65
37836512	8/12/1987	125000	MATHEWS 	65
37836491	8/12/1987	100000	MATHEWS 	49
37836514	7/20/1989	125000	SONIA	65
37836498	7/20/1989	125000	SONIA	49
37836500	7/20/1989	125000	SONIA	49
37836509	7/20/1989	250000	SONIA	49
37836472	7/20/1989	500000	SONIA	65
37836481	7/20/1989	62500	SONIA	65
37836450	7/20/1989	125000	SONIA	65

My code is:

#!/bin/sh
tput clear
tput cup 8 5 
echo -n 'Enter the Minimum Amt  :'
read c

awk -F: '{
       idx = $2 SUBSEP $4 
       arr[idx, ++arrCnt[idx]] = $0
      }
     END {
      for ( i in arrCnt)
      if ( arrCnt > 1 )
      for ( c=1; c<=arrCnt; c++)
      print arr[i,c],TDA[nam]
      }' n2 | sort -t: +3 -4d> n3  

awk -F: '{
          vou[nam]=$1
          dob[nam]=$2
          amt[nam]=$3
          nam=$4
          typ[nam]=$5
         TDA[nam]+=$3
          }
 END {
       for ( nam in TDA )
       if ( TDA[nam] >= '$c' )
     {
       printf"%8d:%30s \n",TDA[nam],nam
      }}' n3 |  sort -t: -n +1 -2d > n4 
 
#awk -F"[ :]" ' NR==FNR { a[$4]=$0;next} a[$4] {print $0}' n4 n3 > n5

Working on LINUX OS.
In my data file it is : seperated .
What I want is, if the amount is , equal to or greater than given amount, then
it should list all the entris of the said person with all the details.With my code I am not getting the desired out put, that is the not only sum total but
along with the voucher number.
With the last line I tried but there is some mistake ?
Can any one help ??
Thanks in advance .

You have about 60+ posts but still you are not using code tags. Can you please edit your post and add

code tags

?

1 Like

Should the separator be space instead in your awk code?

awk -F" "

A single space is the default delimiter in awk

vakharia Mahesh no apologies please, you have not committed any sin. Just try to use CODE tags when necessary.

How about this,

awk '$3>=3000000{print;a+=$3} END {print "Total Amount: " a} ' infile

Hi Pravin27,
Thanks Pravin27 for your reply , but Sorry , what I wanted is , if the sum total of the particular named person amount is equal to or greater then the given total (say in my example >= 3000000), then print all the detail , I am trying to get back the all details by the last line of my code where I have put # at present ,but failed to get the correct data .

(1) By using array I am sorting the data where the DOB and Name are same and if the person's name is repeated then only those records are extracted.
(2) Using awk to sum the Amount ,
(3) After the summing, I want the full details if the sum amount is equal to or
greater then the replied in the begining of the code.

So I wanted if any one can show where I am making mistake? :wall:

Try like this, Read input file twice

awk 'NR==FNR{n[$4]+=$3;next} n[$4]>=3000000{print}' infile infile

Hi Pravin27 ,
Thanks lot for your reply, I tried your code , twice the input file , but not getting the out put at all . Nothing in output.Thanks once agaiin . I think The vertical total and then getting the full details by awk prog may not be possible.??

Thank you very much for sparing time for me.

See the below code,

$cat infile
voucher    DOB           amount    name                              type
======   =======    =======  ==================   ===
37836452        1/13/1961       125000  ASHUTOSH        65
37836505        1/13/1961       500000  ASHUTOSH        49
37836455        1/13/1961       100000  ASHUTOSH        65
37836488        1/13/1961       250000  ASHUTOSH        75
37836473        1/13/1961       125000  ASHUTOSH        49
37836478        1/13/1961       250000  ASHUTOSH        49
37836519        1/13/1961       150000  ASHUTOSH        49
37836437        1/13/1961       500000  ASHUTOSH        49
37836461        1/13/1961       125000  ASHUTOSH        65
37836477        1/13/1961       250000  ASHUTOSH        65
37836486        1/13/1961       250000  ASHUTOSH        65
37836513        1/13/1961       500000  ASHUTOSH        65
37836431        1/13/1961       125000  ASHUTOSH        65
37836427        5/31/1991       350000  JOHNSON 75
37836470        5/31/1991       125000  JOHNSON 75
37836511        5/31/1991       62500   JOHNSON 75
37836520        5/31/1991       500000  JOHNSON 75
37836484        5/31/1991       200000  JOHNSON 75
37836435        5/31/1991       250000  JOHNSON 75
37836433        5/31/1991       150000  JOHNSON 65
37836482        5/31/1991       100000  JOHNSON 78
37836516        5/31/1991       100000  JOHNSON 65
37836497        5/31/1991       62500   JOHNSON 65
37836453        5/31/1991       375000  JOHNSON 75
37836506        7/12/1983       125000  LEENA   75
37836499        7/12/1983       125000  LEENA   75
37836507        7/12/1983       115000  LEENA   75
37836521        7/12/1983       250000  LEENA   65
37836465        7/12/1983       62500   LEENA   65
37836489        7/12/1983       500000  LEENA   49
37836459        7/12/1983       62500   LEENA   65
37836429        7/12/1983       250000  LEENA   65
37836436        7/12/1983       500000  LEENA   65
37836447        7/12/1983       500000  LEENA   65
37836476        8/12/1987       250000  MATHEWS         65
37836469        8/12/1987       100000  MATHEWS         75
37836454        8/12/1987       500000  MATHEWS         75
37836460        8/12/1987       375000  MATHEWS         75
37836522        8/12/1987       125000  MATHEWS         65
37836467        8/12/1987       62500   MATHEWS         65
37836446        8/12/1987       125000  MATHEWS         49
37836430        8/12/1987       62500   MATHEWS         49
37836449        8/12/1987       75000   MATHEWS         49
37836475        8/12/1987       125000  MATHEWS         49
37836474        8/12/1987       125000  MATHEWS         49
37836457        8/12/1987       200000  MATHEWS         89
37836466        8/12/1987       125000  MATHEWS         65
37836451        8/12/1987       125000  MATHEWS         65
37836512        8/12/1987       125000  MATHEWS         65
37836491        8/12/1987       100000  MATHEWS         49
37836514        7/20/1989       125000  SONIA   65
37836498        7/20/1989       125000  SONIA   49
37836500        7/20/1989       125000  SONIA   49
37836509        7/20/1989       250000  SONIA   49
37836472        7/20/1989       500000  SONIA   65
37836481        7/20/1989       62500   SONIA   65
37836450        7/20/1989       125000  SONIA   65
$awk 'NR==FNR{n[$4]+=$3;next} n[$4]>=3000000{print}' infile infile

O/P

37836452        1/13/1961       125000  ASHUTOSH        65
37836505        1/13/1961       500000  ASHUTOSH        49
37836455        1/13/1961       100000  ASHUTOSH        65
37836488        1/13/1961       250000  ASHUTOSH        75
37836473        1/13/1961       125000  ASHUTOSH        49
37836478        1/13/1961       250000  ASHUTOSH        49
37836519        1/13/1961       150000  ASHUTOSH        49
37836437        1/13/1961       500000  ASHUTOSH        49
37836461        1/13/1961       125000  ASHUTOSH        65
37836477        1/13/1961       250000  ASHUTOSH        65
37836486        1/13/1961       250000  ASHUTOSH        65
37836513        1/13/1961       500000  ASHUTOSH        65
37836431        1/13/1961       125000  ASHUTOSH        65

Please post your input file and code which you tried.

1 Like

Thanks Pravin27,
Following is the code I have tried .bit changed from my original which is there in the first post.

#!/bin/sh
tput clear
tput cup 8 5 
echo -n 'Enter the Amt  file name:'
read c

awk -F: '{
       idx = $2 SUBSEP $4 
       arr[idx, ++arrCnt[idx]] = $0
      }
     END {
      for ( i in arrCnt)
      if ( arrCnt > 1 )
      for ( c=1; c<=arrCnt; c++)
      print arr[i,c],TDA[nam]
      }' n2 | sort -t: +3 -4d> n3  

awk -F: '{
          pol[nam]=$1
          dob[nam]=$2
          dab[nam]=$3
          nam=$4
          pln[nam]=$5
         TDA[nam]+=$3
          }
 END {
       for ( nam in TDA )
       if ( TDA[nam] >= '$c' )
     {
       printf"%8d:%30s \n",TDA[nam],nam
      }}' n3 |  sort -t: -n +1 -2d > n4 
 
awk -F"[:]" ' NR == FNR { a[$2]=$0;next} a[$4]{print}' n4 n3 > n5
                                      

Yes your code gave me out put , but it seems there may be some problem in the data , as in the dob format yyyy0601 is most common.

Anyway thanks once again for your kind guidence.