If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > formatting and count in report

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-10-04, 15:15
skd skd is offline
Registered User
 
Join Date: Sep 2003
Posts: 71
Question formatting and count in report

I have a report from which i want to produce new report

here is format of old report
===================
/PA/WASHINGTON/
dbC has a total of 4156 records
File A05233415 has 2 error record(s) with 2 not worked
dbM has a total of 8272 records
/PA/WESTMORELAND/
dbC has a total of 33579 records
File A05233415 has 22 error record(s) with 9 not worked
dbM has a total of 11398 records
/SC/GREENVILLE/
dbC has a total of 877 records
File A06000231 has 1 error record(s) with 1 not worked
File A11060225 has 1 error record(s) with 0 not worked
dbM has a total of 12444 records
/SC/KERSHAW/
dbC has a total of 1479 records
dbM has a total of 2615 records

Desired new format
===============
PA/WASHINGTON dbC has a total of 4156 records
PA/WASHINGTON dbM has a total of 8272 records

PA/WESTMORELAND dbC has a total of 33579 records
PA/WESTMORELAND dbM has a total of 11398 records

PA dbC subtotal=37732(4156+33579)
PA dbM subtotal=19670(8272+11398)

SC/GREENVILLE dbC has a total of 877 records
SC/GREENVILLE dbM has a total of 12444 records

SC/KERSHAW dbC has a total of 1479 records
SC/KERSHAW dbM has a total of 2615 records

SC dbC subtotal=2356(877+1479)
SC dbM subtotal=15059(12444+2615)
------------------------------------
total dbC=sum of subtotals of dbC
total dbM=sum of subtotals of dbM


i am not very good in awk but i guess there might be short and easy solution in awk. please advise.
Reply With Quote
  #2 (permalink)  
Old 11-10-04, 16:04
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
can you assume that your 'states' come in sequential bunches?
For example, you CANNOT have this situation:
Quote:
/PA/WASHINGTON/
dbC has a total of 4156 records
File A05233415 has 2 error record(s) with 2 not worked
dbM has a total of 8272 records
/PA/WESTMORELAND/
dbC has a total of 33579 records
File A05233415 has 22 error record(s) with 9 not worked
dbM has a total of 11398 records
/PA/BOSTON/
dbC has a total of 4156 records
File A05233415 has 2 error record(s) with 2 not worked
dbM has a total of 8272 records
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
Reply With Quote
  #3 (permalink)  
Old 11-10-04, 16:17
skd skd is offline
Registered User
 
Join Date: Sep 2003
Posts: 71
Hello vgresh99,
thanks for reading my thread so quick.
I am not quite sure about your question.could you please explain more?
Reply With Quote
  #4 (permalink)  
Old 11-10-04, 16:30
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
ok, start with something like this.
This assumes all of your PAs come in one 'bunch', all of your SCs come in one bunch and so on for the other 'states'

nawk -f skd.awk myFile.txt
Code:
BEGIN {
  SEP_state="/"
}


$1 ~ ("^" SEP_state) {
   stateTown=$0
   split($0, stA, SEP_state)
   if ( FNR==1 )
      state=stA[2]
   else if ( stA[2] != state ) {
      for (i in arr) {
         printf("%s %s subtotal=%d\n", state, i, arr[i]);
         tot[i] += arr[i];
         delete arr[i];
      }
   }
   next;
}

$0 !~ /^File/ {
  print stateTown, $0
  arr[$1] += $(NF-1)
}

END {
  print "------------------------------------------------------";
  for (i in tot)
     printf("total %s=%d\n", i, tot[i])
}
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
Reply With Quote
  #5 (permalink)  
Old 11-10-04, 17:13
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
or better yet:
Code:
BEGIN {
  SEP_state="/"
}

$1 ~ ("^" SEP_state) {
   if ( FNR != 1 && stateTown != $0 )
      printf "\n"
   sub("^" SEP_state,"");sub(SEP_state "$","");
   stateTown=$0;
   split($0, stA, SEP_state)
   if ( FNR==1 )
      state=stA[1]
   else if ( stA[1] != state ) {
      for (i in arr) {
         printf("%s %s subtotal=%d\n", state, i, arr[i]);
         tot[i] += arr[i];
         delete arr[i];
      }
      printf "\n"
   }
   next;
}

$0 !~ /^File/ {
  print stateTown, $0
  arr[$1] += $(NF-1)
}

END {
  print "------------------------------------------------------";
  for (i in tot)
     printf("total %s=%d\n", i, tot[i])
}
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
Reply With Quote
  #6 (permalink)  
Old 11-15-04, 14:15
skd skd is offline
Registered User
 
Join Date: Sep 2003
Posts: 71
sorry for late in testing your code.
there seems a minor problem.
here is my testdata
Reply With Quote
  #7 (permalink)  
Old 11-15-04, 15:35
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
where is it?
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
Reply With Quote
  #8 (permalink)  
Old 11-15-04, 16:11
skd skd is offline
Registered User
 
Join Date: Sep 2003
Posts: 71
oops
i didn't do it right earlier. here is same
Attached Files
File Type: txt testin.txt (133.1 KB, 69 views)
Reply With Quote
  #9 (permalink)  
Old 11-17-04, 15:43
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
sorry for the delay. try this version:
Code:
BEGIN {
  SEP_state="/"
}


$1 ~ ("^" SEP_state) {
   if ( FNR != 1 && stateTown != $0 )
      printf "\n"
   sub("^" SEP_state,"");sub(SEP_state "$","");
   stateTown=$0;
   split($0, stA, SEP_state)
   if ( FNR==1 )
      state=stA[1]
   else if ( stA[1] != state ) {
      for (i in arr) {
         printf("%s %s subtotal=%d\n", state, i, arr[i]);
         tot[i] += arr[i];
         delete arr[i];
      }
      printf "\n"
   }
   next;
}

$0 !~ /^[ ]*File/ && /database has a total of/ {
  print stateTown, $0
  arr[$1] += $(NF-1)
}

END {
  print "------------------------------------------------------";
  for (i in tot)
     printf("total %s=%d\n", i, tot[i])
}
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
Reply With Quote
  #10 (permalink)  
Old 11-17-04, 16:12
skd skd is offline
Registered User
 
Join Date: Sep 2003
Posts: 71
seems working fine
is it possible to print subtotal line only one time right before value of $state changes?
Reply With Quote
  #11 (permalink)  
Old 11-17-04, 16:29
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
Quote:
Originally Posted by skd
seems working fine
is it possible to print subtotal line only one time right before value of $state changes?
do you mean instead of having:
Quote:
SC dbC subtotal=2356
SC dbM subtotal=15059
to have:
Quote:
SC subtotal=17415(2356+15059)
??
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
Reply With Quote
  #12 (permalink)  
Old 11-18-04, 08:54
skd skd is offline
Registered User
 
Join Date: Sep 2003
Posts: 71
something like this.

AR/GREENE customer database has a total of 11 records
AR/GREENE street database has a total of 6 records

AR/WOODRUFF customer database has a total of 2 records
AR/WOODRUFF street database has a total of 2 records

AR street subtotal=48042 // Pefect here
AR customer subtotal=168433 // Pefect here


MO/ANDREW customer database has a total of 106 records
MO/ANDREW street database has a total of 83 records

AR street subtotal=83 // should NOT write this
AR customer subtotal=106 // should NOT write this

MO/AUDRAIN customer database has a total of 2376 records
MO/AUDRAIN street database has a total of 377 records

AR street subtotal=377 // should NOT write this
AR customer subtotal=2376 // should NOT write this



|
|
|
|
|

MO/MCDONALD customer database has a total of 77 records
MO/MCDONALD street database has a total of 46 records

AR street subtotal=46 //should write MO Street subtotal=
AR customer subtotal=77 //should write MO customer subtotal=

MS/MARION customer database has a total of 15 records
MS/MARION street database has a total of 5 records

also subtotal count is not coming correct when state changes from AR to MO and so on.
Reply With Quote
  #13 (permalink)  
Old 11-18-04, 09:51
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
sorry 'bout that - try this version:
Code:
BEGIN {
  SEP_state="/"
}


$1 ~ ("^" SEP_state) {
   if ( FNR != 1 && stateTown != $0 )
      printf "\n"
   sub("^" SEP_state,"");sub(SEP_state "$","");
   stateTown=$0;
   split($0, stA, SEP_state)
   if ( FNR==1 )
      state=stA[1]
   else if ( stA[1] != state ) {
      for (i in arr) {
         printf("%s %s subtotal=%d\n", state, i, arr[i]);
         tot[i] += arr[i];
         delete arr[i];
      }
      state = stA[1];
      printf "\n"
   }
   next;
}

$0 !~ /^[ ]*File/ && /database has a total of/ {
  print stateTown, $0
  arr[$1] += $(NF-1)
}

END {
    printf "\n"
    for (i in arr) {
       printf("%s %s subtotal=%d\n", state, i, arr[i]);
       tot[i] += arr[i];
       delete arr[i];
    }

  print "------------------------------------------------------";
  for (i in tot)
     printf("total %s=%d\n", i, tot[i])
}
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+

Last edited by vgersh99; 11-18-04 at 09:59.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On