Help using awk with a text file


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Help using awk with a text file
# 15  
Old 04-03-2013
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.
# 16  
Old 04-03-2013
awk testing

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.
Code:
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 ----------

Code:
     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

# 17  
Old 04-03-2013
If I have interpreted your new requirements correctly, the following should do what you want:
Code:
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.
This User Gave Thanks to Don Cragun For This Post:
# 18  
Old 04-03-2013
Awk testing

Don, Yes indeed that worked fine. Thank you for your outstanding work and I appreciate your patience working with me.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. UNIX for Beginners Questions & Answers

awk to update file with partial matching line in another file and append text

In the awk below I am trying to cp and paste each matching line in f2 to $3 in f1 if $2 of f1 is in the line in f2 somewhere. There will always be a match (usually more then 1) and my actual data is much larger (several hundreds of lines) in both f1 and f2. When the line in f2 is pasted to $3 in... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

Splitting a text file into smaller files with awk, how to create a different name for each new file

Hello, I have some large text files that look like, putrescine Mrv1583 01041713302D 6 5 0 0 0 0 999 V2000 2.0928 -0.2063 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0 5.6650 0.2063 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0 3.5217 ... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

4. Shell Programming and Scripting

awk to reformat text file

Howdy. AWK beginner here. I need to reformat a text file in the following format: TTGS08-2014001 6018.00 143563.00 ... (2 Replies)
Discussion started by: c47v3770
2 Replies

5. Shell Programming and Scripting

Search and replace from file in awk using a 16 bit text file

Hello, Some time ago a helpful awk file was provided on the forum which I give below: NR==FNR{A=$0;next}{for(j in A){split(A,P,"=");for(i=1;i<=NF;i++){if($i==P){$i=P}}}}1 While it works beautifully on English and Latin characters i.e. within the ASCII range of 127, the moment a character beyond... (6 Replies)
Discussion started by: gimley
6 Replies

6. Shell Programming and Scripting

Awk to convert a text file to CSV file with some string manipulation

Hi , I have a simple text file with contents as below: 12345678900 971,76 4234560890 22345678900 5971,72 5234560990 32345678900 71,12 6234560190 the new csv-file should be like: Column1;Column2;Column3;Column4;Column5 123456;78900;971,76;423456;0890... (9 Replies)
Discussion started by: FreddyDaKing
9 Replies

7. UNIX for Advanced & Expert Users

Help using Awk and cut with a text file

Looking for some help on using awk and cut I have a text file that has fixed information and want to write a script that will prompt the user for an account to search for and pint the output The sample line that has the key information looks like this: Statement to: ... (5 Replies)
Discussion started by: ziggy6
5 Replies

8. Shell Programming and Scripting

search text file in file if this file contains necessary text (awk,grep)

Hello friends! Help me pls to write correct awk and grep statements for my task: I have got files with name filename.txt It has such structure: Start of file FROM: address@domen.com (12...890) abc DATE: 11/23/2009 on Std SUBJECT: any subject End of file So, I must check, if this file... (4 Replies)
Discussion started by: candyme
4 Replies

9. Shell Programming and Scripting

awk to reformat a text file

I am definitely not an expert with awk, and I want to reformat a text file like the following. This is probably a very easy one for an expert out there. I would like to keep the lines in the same order, but move the heading to only be listed once above the lines. This is what the text file... (7 Replies)
Discussion started by: linux4life
7 Replies

10. Shell Programming and Scripting

process text file with awk

I have a text file which represent a http packet: header1 haeder2 ..... ..... headern payload I need to count bytes in the payload. How can I get it using awk? Thanks in advance Andrea Musella (2 Replies)
Discussion started by: littleboyblu
2 Replies
Login or Register to Ask a Question