Help using awk with a text file

Basically I have a text file that is made up of customer statements. I want to be able to separate the customer statements into their own file. The file is consistant in that the account # (part of the key) is always on line 10 then on every 55th line from there on out so my logic would be like: Read file put 10th line for charcters 1-5 in variable (account #), compare that to line 65 and if the same, read the next key (65 + 55), if they are different then take 10 lines less and write output using key plus date so key would be 90313.txt for Act 9 on March 2013 statement. I know how to use awk to get it to show x line like awk NR==10 example.txt but I need to know the script to keep this ongoing and to output each statement to its own file.

Can you post some highlighted input data for people to consider please. Expected output would be useful along with any code you have tried so far.

Feel free to sanitise the data, of course.

Robin
Liverpool/Blackburn
UK

              ABC PARTS COMPANY             
             100 WILDFLOWER TRAIL          
             MT PLEASANT SC 29579          
                    800-555-1212           

                                   03/27/13
9    ABC TOWING CO.                  
     123 ANY LANE ST                 
     ACME PA 15610-1084              
                                     

                                              PAGE   1 OF   1

02/05/13 PB 1PB 15.45 15.45
02/05/13 PB 2PB 1029.87 1045.32

                                                      1045.32 
             15.45     1029.87                         

ABC PARTS COMPANY
100 WILDFLOWER TRAIL
MT PLEASANT SC 29579
800-555-1212

                                   03/27/13

103 TONY CEILA
100 ROCKY ROAD
LATROBE PA 15650

                                              PAGE   1 OF   1

02/05/13 PB 4PB 460.00 460.00

                                                       460.00 
                                                460.00 

ABC PARTS COMPANY
100 WILDFLOWER TRAIL
MT PLEASANT SC 29579
800-555-1212

                                   03/27/13

105 RICHARD FELTS
R D 1 BOX 196
MIDDLETOWN NY 10925

                                              PAGE   1 OF   1

02/05/13 PB 4PB 721.56 721.56

                                                       721.56 
                                                721.56 

ABC PARTS COMPANY
100 WILDFLOWER TRAIL
MT PLEASANT SC 29579
800-555-1212

                                   03/27/13

119 A&J HAULING
300 LANE ST
LANCASTER SC 29578

                                              PAGE   1 OF   1

02/05/13 PB 1PB 188.17 188.17

                                                       188.17 
            188.17                                     

ABC PARTS COMPANY
100 WILDFLOWER TRAIL
MT PLEASANT SC 29579
800-555-1212

                                   03/27/13
                                     
                                     
                                     
                                     

                                              PAGE   1 OF   1
















                                                      2415.05 
            203.62     1029.87                 1181.56 
                                                             
                                                             


     FIN CHG              

eCB

I don't have anything written, I am not sure how to write awk statement that will read multiple lines and then output to a file. Basically I am reading line 10, characters c1-5 and putting into a variable ($A) to compare to line 65 characters c1-5 ($NA) and if different then I want to write lines 1 to 55 to file using $A plus month year so file would be 90313.txt. If $A matches on line 65 then read line 120 and compare, if different output lines 1 to 110 to file 90313.txt if not read next 175 line and compare...continue on and on until EOF.

You aren't using CODE tags (which are required when posting code and data to this forum), so we can't identify the lines and characters you're talking about.

You still haven't highlighted the 5 characters on line 10 that you want to compare to line 65 even though this was requested 4 days ago.

Line 10 in all of your input samples appears to be an empty line following the address line:

ACME PA 15610-1084

If we can't match your description of what you're trying to do to the input you're giving us to work with; we can't help.

1 Like

This is a sample of the file with line numbers assigned for easier reading, acct number is on line 10 for the first statement in positions 1 thru 5 then the account # appears on every 55th line after the first account. Each statement is 55 lines long. The goal is to read each statement and write to a separate file for each new statement using the account number as part of the key to the file, the first statement in the example below would be 90313.txt, 2nd statement would be 1000313.txt and so on. I am a newbie at posting so sorry for the first posts...thanks for the assistances!

 1  ^M                 ABC PARTS COMPANY             ^M
 2                   100 WILDFLOWER TRAIL          ^M
 3                   MT PLEASANT SC 29579          ^M
 4                          800-555-1212
 5
 6
 7
 8  ^M                                       04/01/13^M
 9  ^M
10      9    ABC TOWING CO.                  ^M
11           123 ANY LANE ST                 ^M
12           ACME PA 15610-1084              ^M
13                                           ^M
14  ^M
15  ^M
16                                                    PAGE   1 OF   1^M
17  ^M
18  02/05/13         PB  1PB           15.45                    15.45 ^M
19  02/05/13         PB  2PB         1029.87                  1045.32
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48  ^M^M
49                                                            1045.32 ^M
50  ^M
51                   15.45     1029.87                         ^M
52  ^M
53                                   STATEMENT MESSAGE HERE          ^M
54                                   PLEASE RETURN TO US             ^M
55  ^M
56  ^L^M^M                 ABC PARTS COMPANY             ^M
57                   100 WILDFLOWER TRAIL          ^M
58                   MT PLEASANT SC 29579          ^M
59                          800-555-1212
60
61
62
63  ^M                                       04/01/13^M
64  ^M
65    100    MIRIAM ACKERMAN                 ^M
66           193 FESS ROAD                   ^M
67           LATROBE PA  15650               ^M
68                                           ^M
69  ^M
70  ^M
71                                                    PAGE   1 OF   1^M
72  ^M
73  03/29/13         PD    1234        50.00                    50.00 ^M
74  03/29/13         PC                           25.00-        25.00
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103  ^M^M
104                                                              25.00 ^M
105  ^M
106      25.00                                                  ^M
107  ^M
108                                   STATEMENT MESSAGE HERE          ^M
109                                   PLEASE RETURN TO US             ^M
110  ^M
111  ^L^M^M                 ABC PARTS COMPANY             ^M
112                   100 WILDFLOWER TRAIL          ^M
113                   MT PLEASANT SC 29579          ^M
114                          800-555-1212
115
116
117
118  ^M                                       04/01/13^M
119  ^M
120    103    TONY CEILA                      ^M
121           100 ROCKY ROAD                  ^M
122           LATROBE PA 15650                ^M
123                                           ^M
124  ^M
125  ^M
126                                                    PAGE   1 OF   1^M
127  ^M
128  02/05/13         PB  4PB          460.00                   460.00
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158  ^M^M
159                                                             460.00 ^M
160  ^M
161                                                      460.00 ^M
162  ^M
163                                   STATEMENT MESSAGE HERE          ^M
164                                   PLEASE RETURN TO US             ^M
165  ^M
166  ^L^M^M                 ABC PARTS COMPANY             ^M
167                   100 WILDFLOWER TRAIL          ^M
168                   MT PLEASANT SC 29579          ^M
169                          800-555-1212
170
171
172
173  ^M                                       04/01/13^M
174  ^M
175    105    RICHARD FELTS                   ^M
176           R D 1 BOX 196                   ^M
177           MIDDLETOWN NY 10925             ^M
178                                           ^M
179  ^M
180  ^M
181                                                    PAGE   1 OF   1^M
182  ^M
183  02/05/13         PB  4PB          721.56                   721.56
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
207
208
209
210
211
212
213  ^M^M
214                                                             721.56 ^M
215  ^M
216                                                      721.56 ^M
217  ^M
218                                   STATEMENT MESSAGE HERE          ^M
219                                   PLEASE RETURN TO US             ^M
220  ^M

This is a major step in the right direction, but the output file names still have not been specified. Do you really want leading spaces on the output file names when customer numbers are less than five digits? It is easy to put them in (as requested), but it will make handling these files harder for you. Would you prefer to have leading zeroes added so the filename always starts with a 5 digit customer number?

Where do the last 4 digits of the output file names come from? They don't come from the dates on lines 8, 18, or 19 in your 55 line statements. Are they always supposed to be 0313? Are they supposed to be the two digit month and two digit year corresponding to the date when the script is run?

Are the multiple occurrences of the strings "^L" and "^M" in you input file literal characters that you want kept in the output files? Are they a graphic representation of form feed and carriage return characters that you want kept in the output files? Or, are they a graphic representation of form feed and carriage return characters that you want to be stripped from the output?

If there are multiple statements for a given customer number, are they always adjacent records in the input file?

What shell do you want to use and what operating system are you using?

1 Like

This is a major step in the right direction, but the output file names still have not been specified. Do you really want leading spaces on the output file names when customer numbers are less than five digits? It is easy to put them in (as requested), but it will make handling these files harder for you. Would you prefer to have leading zeroes added so the filename always starts with a 5 digit customer number? I would not use leading spaces for the account #, I can either have it with leading zero's or without, I prefer without leading zero's.

Where do the last 4 digits of the output file names come from? They don't come from the dates on lines 8, 18, or 19 in your 55 line statements. Are they always supposed to be 0313? Are they supposed to be the two digit month and two digit year corresponding to the date when the script is run? The last 4 digits will come from the system time and date which will always be run on the last day of the month so I would use month and year. I usually use the following to come up with my date,
date '+ %c/%m/%d' > date hold
y=`cat date.hold | cut -c24-25`
m=`cat date.hold | cut -c27 -c28`
then I would use variables $y and $m in naming the file

Are the multiple occurrences of the strings "^L" and "^M" in you input file literal characters that you want kept in the output files? Are they a graphic representation of form feed and carriage return characters that you want kept in the output files? Or, are they a graphic representation of form feed and carriage return characters that you want to be stripped from the output? As you noted they are carriage return and form feed and I would want them in the output file as well

If there are multiple statements for a given customer number, are they always adjacent records in the input file? Yes, if a statement is more than 1 page it would be adjacent to the first statement so if account 9 had multiple pages one would follow after the other.

What shell do you want to use and what operating system are you using? SCO 5.0.7 and using the bourne shell

The following awk script seems to do what you want:

awk -v my="$(date "+%m%y")" '
{       out = out $0 "\n"
}
(NR % 55) == 10 {
        CN = substr($0, 1, 5)
        if(lCN != CN) {
                if(fn != "") close(fn)
                fn = sprintf("%d%s.txt", lCN = CN, my)
        }
        next
}
(NR % 55) == 0 {
        printf("%s", out) > fn
        out = ""
}
END {   if(fn != "") close(fn)
}' input

Note that the 1st line of your sample input file does not start with a form feed character. So, if you cat the output files to your printer, you will be missing a form feed before the file that comes from the first record of you input file. With you sample input and your choice not to have leading zeros in output file names, the last file printed will be missing the form feed.

This probably doesn't matter for an SCO system, but if someone else wants to try this script on a Solaris/SunOS system, they should use /usr/xpg4/bin/awk or nawk instead of awk .

Well it is real close but I must be missing a quote in the awk script because it is using the literal of the month and year instead of populating with the real month and year. Did I miss a tick on the copy and paste?
933 Apr 2 17:08 100$(date +%m%y).txt
866 Apr 2 17:08 103$(date +%m%y).txt
866 Apr 2 17:08 105$(date +%m%y).txt

awk -v my="$(date "+%m%y")" '
{       out = out $0 "\n"
}
(NR % 55) == 10 {
        CN = substr($0, 1, 5)
        if(lCN != CN) {
                if(fn != "") close(fn)
                fn = sprintf("%d%s.txt", lCN = CN, my)
        }
        next
}
(NR % 55) == 0 {
        printf("%s", out) > fn
        out = ""
}
END {   if(fn != "") close(fn)
}' report10.txt

With the input you provided, I get the files:

1000413.txt
1030413.txt
1050413.txt
90413.txt

when I run this script on OS X.

Are you sure that the first line of the script you ran was:

awk -v my="$(date "+%m%y")" '

and not:

awk -v my='$(date '+%m%y')' '

?
If you used single quotes where I had double quotes in the first line, the command substitution would not be performed when assigning a value to the variable my and you would end up with:

100$(date +%m%y).txt
103$(date +%m%y).txt
105$(date +%m%y).txt
9$(date +%m%y).txt

as the output file names.

1 Like

Yes I had the quotes. I modified it to this and it worked fine!

date '+ %c/%m/%d' >date.hold
y=`cat date.hold | cut -c22-25`
m=`cat date.hold | cut -c27-28`
awk  '
{       out = out $0 "\n"
}
(NR % 55) == 10 {
        CN = substr($0, 1, 5)
        if(lCN != CN) {
                if(fn != "") close(fn)
                fn = sprintf("%d%s.txt", lCN = CN, my)
        }
        next
}
(NR % 55) == 0 {
        printf("%s", out) > fn
        out = ""
}
END {   if(fn != "") close(fn)
}' report10.txt

---------- Post updated at 06:51 PM ---------- Previous update was at 06:50 PM ----------

A big thank you to Don for excellent assistance on the script, much appreciated! :slight_smile:

Note that you're setting shell variables m and y , but you are not setting the awk variable my .

Even if your shell doesn't recognize $(...) , it would still be much easier to set the month and year in a single invocation of date instead of using date once, two calls to cat, and two calls to cut. Please try changing the first line of my original script to:

awk -v my=`date +%m%y` '

if your shell doesn't recognize the $(...) form of command substitution. Note that it is important that you use the exact single quote and back quotes as shown.

1 Like

Ok that new statement worked,

awk -v my=`date +%m%y` '

I did some more testing to check what would happen with an account that had more than 1 page and it wants to break the statement up in to separate file

Here is an example of one account with 3 pages

    1  ^M                 ABC PARTS COMPANY             ^M
    2                   100 WILDFLOWER TRAIL          ^M
    3                   MT PLEASANT SC 29579          ^M
    4                          800-555-1212
    5
    6
    7
    8  ^M                                       02/05/13^M
    9  ^M
   10    960    FRONTIER CONSTRUCTION           ^M
   11           P O BOX 84                      ^M
   12           JONES MILLS PA 15646            ^M
   13                                           ^M
   14  ^M
   15  ^M
   16                                                    PAGE   1 OF   3^M
   17  ^M
   18  10/31/12         PB  1PB         8304.54                  8304.54 ^M
   19  10/31/12         PB  2PB         3123.15                 11427.69 ^M
   20  11/01/12         ID  146469       589.75                 12017.44 ^M
   21  11/03/12         ID  146502       275.49                 12292.93 ^M
   22  11/03/12         ID  146503        30.69                 12323.62 ^M
   23  11/05/12         ID  146509       417.85                 12741.47 ^M
   24  11/05/12         ID  146515        75.68                 12817.15 ^M
   25  11/05/12         ID  146516       598.90                 13416.05 ^M
   26  11/05/12         ID  146518       275.49                 13691.54 ^M
   27  11/05/12         ID  146519       158.95                 13850.49 ^M
   28  11/05/12         ID  146520        22.79                 13873.28 ^M
   29  11/05/12         ID  146525        11.13                 13884.41 ^M
   30  11/06/12         ID  146539        90.31                 13974.72 ^M
   31  11/06/12         IC  146547                   19.19-     13955.53 ^M
   32  11/07/12         ID  146552       401.37                 14356.90 ^M
   33  11/07/12         ID  146558       110.18                 14467.08 ^M
   34  11/08/12         ID  146583       615.84                 15082.92 ^M
   35  11/09/12         ID  146590       253.76                 15336.68 ^M
   36  11/12/12         ID  146612       122.72                 15459.40 ^M
   37  11/12/12         ID  146618       143.65                 15603.05 ^M
   38  11/13/12         ID  146620       691.25                 16294.30 ^M
   39  11/13/12         ID  146635        42.35                 16336.65 ^M
   40  11/14/12         ID  146646        16.94                 16353.59 ^M
   41  11/14/12         ID  146653       593.55                 16947.14 ^M
   42  11/14/12         ID  146656       221.88                 17169.02 ^M
   43  11/14/12         ID  146661        90.74                 17259.76 ^M
   44  11/14/12         ID  146662       200.34                 17460.10 ^M
   45  11/14/12         ID  146664        67.03                 17527.13 ^M
   46  11/15/12         ID  146681      2403.09                 19930.22 ^M
   47  11/15/12         ID  146682        35.37                 19965.59 ^L
                ABC PARTS COMPANY             ^M
   48                   100 WILDFLOWER TRAIL          ^M
   49                   MT PLEASANT SC 29579          ^M
   50                          800-555-1212
   51
   52
   53
   54  ^M                                       02/05/13^M
   55  ^M
   56    960    FRONTIER CONSTRUCTION           ^M
   57           P O BOX 84                      ^M
   58           JONES MILLS PA 15646            ^M
   59                                           ^M
   60  ^M
   61  ^M
   62                                                    PAGE   2 OF   3^M
   63  ^M
   64  11/15/12         ID  146684        74.19                 20039.78 ^M
   65  11/16/12         ID  146702       254.08                 20293.86 ^M
   66  11/16/12         ID  146703       299.34                 20593.20 ^M
   67  11/19/12         ID  146719       227.54                 20820.74 ^M
   68  11/19/12         ID  146722        38.01                 20858.75 ^M
   69  11/19/12         ID  146733       142.36                 21001.11 ^M
   70  11/20/12         ID  146740        64.43                 21065.54 ^M
   71  11/20/12         ID  146753       145.62                 21211.16 ^M
   72  11/20/12         ID  146757       314.13                 21525.29 ^M
   73  11/20/12         ID  146761        59.71                 21585.00 ^M
   74  11/20/12         ID  146763        31.84                 21616.84 ^M
   75  11/21/12         PC  CK6192                   16.11-     21600.73 ^M
   76  11/21/12         PC  CK4956                 8561.65-     13039.08 ^M
   77  11/23/12         ID  146789        46.80                 13085.88 ^M
   78  11/23/12         ID  146795        46.48                 13132.36 ^M
   79  11/23/12         ID  146805      1004.47                 14136.83 ^M
   80  11/26/12         ID  146812       100.40                 14237.23 ^M
   81  11/27/12         ID  146819       139.87                 14377.10 ^M
   82  11/27/12         ID  146820       279.29                 14656.39 ^M
   83  11/27/12         ID  146821        48.37                 14704.76 ^M
   84  11/27/12         ID  146822        72.81                 14777.57 ^M
   85  11/27/12         ID  146823        71.24                 14848.81 ^M
   86  11/27/12         ID  146826       156.46                 15005.27 ^M
   87  11/28/12         ID  146828        95.24                 15100.51 ^M
   88  11/28/12         ID  146829        76.79                 15177.30 ^M
   89  11/28/12         ID  146830       163.02                 15340.32 ^M
   90  11/28/12         ID  146842        13.78                 15354.10 ^M
   91  11/28/12         ID  146854       254.83                 15608.93 ^M
   92  11/28/12         ID  146856       915.88                 16524.81 ^M
   93  11/29/12         ID  146877       137.75                 16662.56 ^L
                ABC PARTS COMPANY             ^M
   94                   100 WILDFLOWER TRAIL          ^M
   95                   MT PLEASANT SC 29579          ^M
   96                          800-555-1212
   97
   98
   99
  100  ^M                                       02/05/13^M
  101  ^M
  102    960    FRONTIER CONSTRUCTION           ^M
  103           P O BOX 84                      ^M
  104           JONES MILLS PA 15646            ^M
  105                                           ^M
  106  ^M
  107  ^M
  108                                                    PAGE   3 OF   3^M
  109  ^M
  110  11/29/12         ID  146878        34.27                 16696.83 ^M
  111  11/29/12         ID  146879        33.92                 16730.75 ^M
  112  11/29/12         ID  146880       178.06                 16908.81 ^M
  113  11/29/12         ID  146881        75.83                 16984.64 ^M
  114  11/29/12         ID  146882       148.44                 17133.08
  115
  116
  117
  118
  119
  120
  121
  122
  123
  124
  125
  126
  127
  128
  129
  130
  131
  132
  133
  134
  135
  136
  137
  138
  139
  140  ^M^M
  141                                                           17133.08 ^M
  142  ^M
  143   14283.15      2849.93                                     ^M
  144  ^M
  145                                   STATEMENT MESSAGE HERE          ^M
  146                                   PLEASE RETURN TO US             ^M
  147  ^M
 

The results ended in two files
2849 Apr 3 08:42 110413.txt
2787 Apr 3 08:42 9600413.txt

You said all statements contained exactly 55 lines. You said the customer number always appeared on lines where the line number mod 55 was 10.

This is a completely different problem.

Please explain how the program is now supposed to identify the first or last line of a customer statement. I could make some guesses, but I think you need to figure out the logic involved and write a new specification explaining how statement boundaries are determined and grouped into files. And a sample input file with only one statement is not going to be enough to figure out the logic involved.

Sorry for the confusion. I didn't realize that it handled a multiple page statement differently so with that said I changed the output of the statement to a different format that should be easier to work with. I have an example of 3 accounts and one has two pages. This should be separated into 3 separate files, one for acount 951, one for 960 and one for 961.

Statement from:                        Statement message:
       ABC PARTS COMPANY                    STATEMENT MESSAGE HERE
       100 WILDFLOWER TRAIL                 PLEASE RETURN TO US
       MT PLEASANT SC 29579                 NOW OR LATE PAYMENT WILL BE ASSE
              800-555-1212
Statement to:                          Account Number:   951
     W W FRIEDLINE GARAGE
     1027 PLEASANT HILL RD
     SOMERSET PA  15501

Statement date:  02/05/13              Page:     1 of:    1
Invoice#    Date     PO#/CK#     Credit   Code    Debit    Balance
  1PB       10/31/12                      PB     108.15     108.15
  2PB       10/31/12                      PB     943.69    1051.84
  3PB       10/31/12                      PB      92.61    1144.45
  4PB       10/31/12                      PB    6173.85    7318.30
  999999    02/05/13                      FC     109.77    7428.07
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

                                            Total due:     7428.07
 
          CURRENT       30 DAYS       60 DAYS     90 & OVER
           109.77        108.15        943.69       6266.46

Statement from:                        Statement message:
       ABC PARTS COMPANY                    STATEMENT MESSAGE HERE
       100 WILDFLOWER TRAIL                 PLEASE RETURN TO US
       MT PLEASANT SC 29579                 NOW OR LATE PAYMENT WILL BE ASSE
              800-555-1212
Statement to:                          Account Number:   960
     FRONTIER CONSTRUCTION
     P O BOX 84
     JONES MILLS PA 15646

Statement date:  02/05/13              Page:     1 of:    2
Invoice#    Date     PO#/CK#     Credit   Code    Debit    Balance
  1PB       10/31/12                      PB    8304.54    8304.54
  2PB       10/31/12                      PB    3123.15   11427.69
  146469    11/01/12                      ID     589.75   12017.44
  146502    11/03/12                      ID     275.49   12292.93
  146503    11/03/12                      ID      30.69   12323.62
  146509    11/05/12                      ID     417.85   12741.47
  146515    11/05/12                      ID      75.68   12817.15
  146516    11/05/12                      ID     598.90   13416.05
  146518    11/05/12                      ID     275.49   13691.54
  146519    11/05/12                      ID     158.95   13850.49
  146520    11/05/12                      ID      22.79   13873.28
  146525    11/05/12                      ID      11.13   13884.41
  146539    11/06/12                      ID      90.31   13974.72
  146547    11/06/12              19.19-  IC              13955.53
  146552    11/07/12                      ID     401.37   14356.90
  146558    11/07/12                      ID     110.18   14467.08
  146583    11/08/12                      ID     615.84   15082.92
  146590    11/09/12                      ID     253.76   15336.68
  146612    11/12/12                      ID     122.72   15459.40
  146618    11/12/12                      ID     143.65   15603.05
  146620    11/13/12                      ID     691.25   16294.30
  146635    11/13/12                      ID      42.35   16336.65
  146646    11/14/12                      ID      16.94   16353.59
  146653    11/14/12                      ID     593.55   16947.14
  146656    11/14/12                      ID     221.88   17169.02
  146661    11/14/12                      ID      90.74   17259.76
  146662    11/14/12                      ID     200.34   17460.10
  146664    11/14/12                      ID      67.03   17527.13
  146681    11/15/12                      ID    2403.09   19930.22
  146682    11/15/12                      ID      35.37   19965.59
  146684    11/15/12                      ID      74.19   20039.78
  146702    11/16/12                      ID     254.08   20293.86
  146703    11/16/12                      ID     299.34   20593.20
  146719    11/19/12                      ID     227.54   20820.74
 
Statement from:                        Statement message:
       ABC PARTS COMPANY                    STATEMENT MESSAGE HERE
       100 WILDFLOWER TRAIL                 PLEASE RETURN TO US
       MT PLEASANT SC 29579                 NOW OR LATE PAYMENT WILL BE ASSE
              800-555-1212
Statement to:                          Account Number:   960
     FRONTIER CONSTRUCTION
     P O BOX 84
     JONES MILLS PA 15646

Statement date:  02/05/13              Page:     2 of:    2
Invoice#    Date     PO#/CK#     Credit   Code    Debit    Balance
  146722    11/19/12                      ID      38.01   20858.75
  146733    11/19/12                      ID     142.36   21001.11
  146740    11/20/12                      ID      64.43   21065.54
  146753    11/20/12                      ID     145.62   21211.16
  146757    11/20/12                      ID     314.13   21525.29
  146761    11/20/12                      ID      59.71   21585.00
  146763    11/20/12                      ID      31.84   21616.84
  CK6192    11/21/12              16.11-  PC              21600.73
  CK4956    11/21/12            8561.65-  PC              13039.08
  146789    11/23/12                      ID      46.80   13085.88
  146795    11/23/12                      ID      46.48   13132.36
  146805    11/23/12                      ID    1004.47   14136.83
  146812    11/26/12                      ID     100.40   14237.23
  146819    11/27/12                      ID     139.87   14377.10
  146820    11/27/12                      ID     279.29   14656.39
  146821    11/27/12                      ID      48.37   14704.76
  146822    11/27/12                      ID      72.81   14777.57
  146823    11/27/12                      ID      71.24   14848.81
  146826    11/27/12                      ID     156.46   15005.27
  146828    11/28/12                      ID      95.24   15100.51
  146829    11/28/12                      ID      76.79   15177.30
  146830    11/28/12                      ID     163.02   15340.32
  146842    11/28/12                      ID      13.78   15354.10
  146854    11/28/12                      ID     254.83   15608.93
  146856    11/28/12                      ID     915.88   16524.81
  146877    11/29/12                      ID     137.75   16662.56
  146878    11/29/12                      ID      34.27   16696.83
  146879    11/29/12                      ID      33.92   16730.75
  146880    11/29/12                      ID     178.06   16908.81
  146881    11/29/12                      ID      75.83   16984.64
  146882    11/29/12                      ID     148.44   17133.08
 
 

                                            Total due:    17133.08
 
          CURRENT       30 DAYS       60 DAYS     90 & OVER
         14283.15       2849.93

Statement from:                        Statement message:
       ABC PARTS COMPANY                    STATEMENT MESSAGE HERE
       100 WILDFLOWER TRAIL                 PLEASE RETURN TO US
       MT PLEASANT SC 29579                 NOW OR LATE PAYMENT WILL BE ASSE
              800-555-1212
Statement to:                          Account Number:   961
     FRONTIER TRANSPORT
     P O BOX 84
     JONES MILLS PA.15646

Statement date:  02/05/13              Page:     1 of:    1
Invoice#    Date     PO#/CK#     Credit   Code    Debit    Balance
  1PB       10/31/12                      PB    1142.97    1142.97
  146613    11/12/12                      ID      21.95    1164.92
  146701    11/16/12                      ID      82.65    1247.57
  CK4956    11/21/12              35.95-  PC               1211.62
  146832    11/28/12                      ID      59.95    1271.57
  146836    11/28/12                      ID      59.95    1331.52
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
                                            Total due:     1331.52
 
          CURRENT       30 DAYS       60 DAYS     90 & OVER
           224.50       1107.02

---------- Post updated at 11:38 AM ---------- Previous update was at 11:31 AM ----------

     1  ^M
     2  ^M
     3  Statement from:                        Statement message:^M
     4  ^M
     5         ABC PARTS COMPANY                    STATEMENT MESSAGE HERE
    ^M
     6         100 WILDFLOWER TRAIL                 PLEASE RETURN TO US
    ^M
     7         MT PLEASANT SC 29579                 NOW OR LATE PAYMENT WILL BE
ASSE^M
     8                800-555-1212           ^M
     9  ^M
    10  Statement to:                          Account Number:   951^M
    11  ^M
    12       W W FRIEDLINE GARAGE            ^M
    13       1027 PLEASANT HILL RD           ^M
    14       SOMERSET PA  15501              ^M
    15                                       ^M
    16  ^M
    17  Statement date:  02/05/13              Page:     1 of:    1^M
    18  ^M
    19  Invoice#    Date     PO#/CK#     Credit   Code    Debit    Balance^M
    20  ^M
    21    1PB       10/31/12                      PB     108.15     108.15 ^M
    22    2PB       10/31/12                      PB     943.69    1051.84 ^M
    23    3PB       10/31/12                      PB      92.61    1144.45 ^M
    24    4PB       10/31/12                      PB    6173.85    7318.30 ^M
    25    999999    02/05/13                      FC     109.77    7428.07 ^M
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54  ^M^M
    55  ^M
    56  ^M
    57  ^M
    58                                              Total due:     7428.07 ^M
    59  ^M
    60  ^M
    61  ^M
    62            CURRENT       30 DAYS       60 DAYS     90 & OVER^M
    63             109.77        108.15        943.69       6266.46 ^L^M^M
    64  ^M
    65  Statement from:                        Statement message:^M
    66  ^M
    67         ABC PARTS COMPANY                    STATEMENT MESSAGE HERE
    ^M
    68         100 WILDFLOWER TRAIL                 PLEASE RETURN TO US
    ^M
    69         MT PLEASANT SC 29579                 NOW OR LATE PAYMENT WILL BE
ASSE^M
    70                800-555-1212           ^M
    71  ^M
    72  Statement to:                          Account Number:   960^M
    73  ^M
    74       FRONTIER CONSTRUCTION           ^M
    75       P O BOX 84                      ^M
    76       JONES MILLS PA 15646            ^M
    77                                       ^M
    78  ^M
    79  Statement date:  02/05/13              Page:     1 of:    2^M
    80  ^M
    81  Invoice#    Date     PO#/CK#     Credit   Code    Debit    Balance^M
    82  ^M
    83    1PB       10/31/12                      PB    8304.54    8304.54 ^M
    84    2PB       10/31/12                      PB    3123.15   11427.69 ^M
    85    146469    11/01/12                      ID     589.75   12017.44 ^M
    86    146502    11/03/12                      ID     275.49   12292.93 ^M
    87    146503    11/03/12                      ID      30.69   12323.62 ^M
    88    146509    11/05/12                      ID     417.85   12741.47 ^M
    89    146515    11/05/12                      ID      75.68   12817.15 ^M
    90    146516    11/05/12                      ID     598.90   13416.05 ^M
    91    146518    11/05/12                      ID     275.49   13691.54 ^M
    92    146519    11/05/12                      ID     158.95   13850.49 ^M
    93    146520    11/05/12                      ID      22.79   13873.28 ^M
    94    146525    11/05/12                      ID      11.13   13884.41 ^M
    95    146539    11/06/12                      ID      90.31   13974.72 ^M
    96    146547    11/06/12              19.19-  IC              13955.53 ^M
    97    146552    11/07/12                      ID     401.37   14356.90 ^M
    98    146558    11/07/12                      ID     110.18   14467.08 ^M
    99    146583    11/08/12                      ID     615.84   15082.92 ^M
   100    146590    11/09/12                      ID     253.76   15336.68 ^M
   101    146612    11/12/12                      ID     122.72   15459.40 ^M
   102    146618    11/12/12                      ID     143.65   15603.05 ^M
   103    146620    11/13/12                      ID     691.25   16294.30 ^M
   104    146635    11/13/12                      ID      42.35   16336.65 ^M
   105    146646    11/14/12                      ID      16.94   16353.59 ^M
   106    146653    11/14/12                      ID     593.55   16947.14 ^M
   107    146656    11/14/12                      ID     221.88   17169.02 ^M
   108    146661    11/14/12                      ID      90.74   17259.76 ^M
   109    146662    11/14/12                      ID     200.34   17460.10 ^M
   110    146664    11/14/12                      ID      67.03   17527.13 ^M
   111    146681    11/15/12                      ID    2403.09   19930.22 ^M
   112    146682    11/15/12                      ID      35.37   19965.59 ^M
   113    146684    11/15/12                      ID      74.19   20039.78 ^M
   114    146702    11/16/12                      ID     254.08   20293.86 ^M
   115    146703    11/16/12                      ID     299.34   20593.20 ^M
   116    146719    11/19/12                      ID     227.54   20820.74 ^M
   117  ^L^M^M
   118  ^M
   119  Statement from:                        Statement message:^M
   120  ^M
   121         ABC PARTS COMPANY                    STATEMENT MESSAGE HERE
    ^M
   122         100 WILDFLOWER TRAIL                 PLEASE RETURN TO US
    ^M
   123         MT PLEASANT SC 29579                 NOW OR LATE PAYMENT WILL BE
ASSE^M
   124                800-555-1212           ^M
   125  ^M
   126  Statement to:                          Account Number:   960^M
   127  ^M
   128       FRONTIER CONSTRUCTION           ^M
   129       P O BOX 84                      ^M
   130       JONES MILLS PA 15646            ^M
   131                                       ^M
   132  ^M
   133  Statement date:  02/05/13              Page:     2 of:    2^M
   134  ^M
   135  Invoice#    Date     PO#/CK#     Credit   Code    Debit    Balance^M
   136  ^M
   137    146722    11/19/12                      ID      38.01   20858.75 ^M
   138    146733    11/19/12                      ID     142.36   21001.11 ^M
   139    146740    11/20/12                      ID      64.43   21065.54 ^M
   140    146753    11/20/12                      ID     145.62   21211.16 ^M
   141    146757    11/20/12                      ID     314.13   21525.29 ^M
   142    146761    11/20/12                      ID      59.71   21585.00 ^M
   143    146763    11/20/12                      ID      31.84   21616.84 ^M
   144    CK6192    11/21/12              16.11-  PC              21600.73 ^M
   145    CK4956    11/21/12            8561.65-  PC              13039.08 ^M
   146    146789    11/23/12                      ID      46.80   13085.88 ^M
   147    146795    11/23/12                      ID      46.48   13132.36 ^M
   148    146805    11/23/12                      ID    1004.47   14136.83 ^M
   149    146812    11/26/12                      ID     100.40   14237.23 ^M
   150    146819    11/27/12                      ID     139.87   14377.10 ^M
   151    146820    11/27/12                      ID     279.29   14656.39 ^M
   152    146821    11/27/12                      ID      48.37   14704.76 ^M
   153    146822    11/27/12                      ID      72.81   14777.57 ^M
   154    146823    11/27/12                      ID      71.24   14848.81 ^M
   155    146826    11/27/12                      ID     156.46   15005.27 ^M
   156    146828    11/28/12                      ID      95.24   15100.51 ^M
   157    146829    11/28/12                      ID      76.79   15177.30 ^M
   158    146830    11/28/12                      ID     163.02   15340.32 ^M
   159    146842    11/28/12                      ID      13.78   15354.10 ^M
   160    146854    11/28/12                      ID     254.83   15608.93 ^M
   161    146856    11/28/12                      ID     915.88   16524.81 ^M
   162    146877    11/29/12                      ID     137.75   16662.56 ^M
   163    146878    11/29/12                      ID      34.27   16696.83 ^M
   164    146879    11/29/12                      ID      33.92   16730.75 ^M
   165    146880    11/29/12                      ID     178.06   16908.81 ^M
   166    146881    11/29/12                      ID      75.83   16984.64 ^M
   167    146882    11/29/12                      ID     148.44   17133.08 ^M
   168
   169
   170  ^M^M
   171  ^M
   172  ^M
   173  ^M
   174                                              Total due:    17133.08 ^M
   175  ^M
   176  ^M
   177  ^M
   178            CURRENT       30 DAYS       60 DAYS     90 & OVER^M
   179           14283.15       2849.93                             ^L^M^M
   180  ^M
   181  Statement from:                        Statement message:^M
   182  ^M
   183         ABC PARTS COMPANY                    STATEMENT MESSAGE HERE
    ^M
   184         100 WILDFLOWER TRAIL                 PLEASE RETURN TO US
    ^M
   185         MT PLEASANT SC 29579                 NOW OR LATE PAYMENT WILL BE
ASSE^M
   186                800-555-1212           ^M
   187  ^M
   188  Statement to:                          Account Number:   961^M
   189  ^M
   190       FRONTIER TRANSPORT              ^M
   191       P O BOX 84                      ^M
   192       JONES MILLS PA.15646            ^M
   193                                       ^M
   194  ^M
   195  Statement date:  02/05/13              Page:     1 of:    1^M
   196  ^M
   197  Invoice#    Date     PO#/CK#     Credit   Code    Debit    Balance^M
   198  ^M
   199    1PB       10/31/12                      PB    1142.97    1142.97 ^M
   200    146613    11/12/12                      ID      21.95    1164.92 ^M
   201    146701    11/16/12                      ID      82.65    1247.57 ^M
   202    CK4956    11/21/12              35.95-  PC               1211.62 ^M
   203    146832    11/28/12                      ID      59.95    1271.57 ^M
   204    146836    11/28/12                      ID      59.95    1331.52 ^M
   205
   206
   207
   208
   209
   210
   211
   212
   213
   214
   215
   216
   217
   218
   219
   220
   221
   222
   223
   224
   225
   226
   227
   228
   229
   230
   231
   232  ^M^M
   233  ^M
   234  ^M
   235  ^M
   236                                              Total due:     1331.52 ^M
   237  ^M
   238  ^M
   239  ^M
   240            CURRENT       30 DAYS       60 DAYS     90 & OVER^M
   241             224.50       1107.02                             ^L^M^M

If I have interpreted your new requirements correctly, the following should do what you want:

awk -v my=`(date +%m%y)` -F'[^[:digit:]]*' '
{       out = out $0 "\n"
}
/Account Number:/ {
        CN = $2
        if(lCN != CN) {
                if(fn != "") close(fn)
                fn = sprintf("%d%s.txt", lCN = CN, my)
        }
        next
}
/\f/ {  printf("%s", out) > fn
        out = ""
}
END {   if(fn != "") close(fn)
}' input2

This should work on your SCO system where your shell doesn't recognize $(...) as a form of command substitution.

If anyone wants to try this on a Solaris/SunOS system, they should use /usr/xpg4/bin/awk or nawk instead of awk .

1 Like

Don, Yes indeed that worked fine. Thank you for your outstanding work and I appreciate your patience working with me.