[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 191
  • Last Modified:

Reformatting a text file

Hi,

I am trying to reformat or reaarange a packing list (text file) on my AIX unix machine that our database program produces.  I need to do this outside of the database system as I do not currently have the source code.  I assume the program should be written in perl or awk or sed or??  To make this simple I will show an example of what I have and what I need.  

+++++Begin Existing Invoice++++++++++++
Order Number:  12345    Date: 04/08/05
Ship Via: UPS           Terms: VISA

 Item   Description   Qty  Price
1 34567  Modem          1    100.00
2 31266  Router         2    125.00
3 21890  Phone          1    100.00




Entered By: MRM    
+++++End Existing Invoice++++++++++++++

+++++Begin Invoice Like I Want+++++++++
Date: 04/08/05         Ship Via: UPS
Terms: VISA            Entered By: MRM

 Item   Description   Qty  Price
1 34567  Modem          1    100.00
2 31266  Router         2    125.00
3 21890  Phone          1    100.00




Order Number: 12345
++++++End Invoice Like I Want++++++++++

Note that the number of items can be variable but the Entered By line is always on line 12 of the input.  To make things more complicated, this can be a multipage packing list, so I need to account for a form feed (or count the lines and assume the top of the next page at 14 lines) and then make additional pages as required.

Any thoughts on how to do this?

I will throw in an additional 500 points for a sample script..

Thanks,
Mark
0
9thTee
Asked:
9thTee
  • 3
  • 2
1 Solution
 
ozoCommented:
my($order,$date,$ship,$terms,$entered,@items);
while( <> ){
    ($order,$date) = ($1,$2) if /(Order\s+Number:\s+.*?)\s+(Date:\s+.*)/;
    ($ship,$terms) = ($1,$2) if /(Ship\s+Via:\s+.*?)\s+(Terms:\s+.*)/;
    push @items,$_ if/Item\s+Description\s+Qty\s+Price/..(($entered) = /(Entered\s+By:\s+.*)/);
    if( $entered ){
        pop @items;
        print "$date\t$ship\n$terms\t$entered\n\n",@items,"$order\n";
        @items=();
        $entered = '';
    }
}
0
 
9thTeeAuthor Commented:
Can you briefly tell me how this works?  
Also, how do I run it from a $.  I have perl on my AIX machine but I have not used it.
And does this take into account a multipage packing list?

Thanks,
Mark
0
 
ozoCommented:
It saves the Order Number, Date, Ship Via, Terms, Items, and Entered By information, and when it sees the Entered By line, it prints it all in the desired order.

If you save the above code in a file named "reformat", and you have your existing invoice in a file called "invoice"
then the command
perl reformat invoice > new_invoice
will create a new_invoice file in the new format

You didn't tell me what a multipage packing list looks like so I don't know if they would be handled the way you want, but if there are multiple "Entered By" lines in the input file, it will create multiple invoices like your "Invoice Like I Want " example in the output.
0
 
9thTeeAuthor Commented:
0
 
9thTeeAuthor Commented:
Ok, ozo please see the new question:
http://www.experts-exchange.com/Programming/Programming_Languages/Perl/Q_21383350.html

You answered the current question exactly but I need to change a couple of the assumptions in the original.

Thanks,
Mark
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now