Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Hi ei6eqb and 73 form ka3uca!

I think I have found a program that will do what you need. The program is: Update: Input file format changed per his post

#!/usr/bin/perl use strict; use warnings; 3 == @ARGV or die "USAGE: perl $0 filein fileout shopID\n"; my ($file_in, $file_out, $shopID) = @ARGV; # file_in == file3, file_ou +t == fout.csv shopID == 2345 open my $in, '<', $file_in or die "Unable to open $file_in: $!"; open my $out, '>', $file_out or die "Unable to open $file_out: $!"; chomp(my @hdrs = split /,/, <$in>); my @cols = split /\n/, <<EndCols; Cust Code Inv Num MobileNumber Inv Date PosId PosUserID ShopID Prodcode ProdDiscription ProdDept ProdGroup1 ProdGroup2 Qty Net Amt EndCols print $out "DeviceID,PosTxnID,MobileNumber,Timestamp,PosID,PosUserID,S +hopID,Prodcode,ProdDescription,ProdDept,ProdGroup1,ProdGroup2,Qty,Val +ue\n"; my %tmp; while (<$in>) { next unless /^\S+\s-\s#\s/ .. /^,/; chomp; if (/^,/) { %tmp = (); } elsif (/^(ZB\d+)\s-\s#\s([^,]+)/) { $tmp{Prodcode} = $1; $tmp{ProdDiscription} = $2; $tmp{ShopID} = $shopID; } else { @tmp{@hdrs} = split /,/; $tmp{'Inv Date'} = join("/", reverse split '/', $tmp{'Inv Date +'}); $tmp{'Inv Date'} .= ' 00:00'; print $out join(",", map $_ // '', @tmp{@cols}), "\n"; } } close $in or die $!; close $out or die $!
Run from the command line like perl test2.pl file3 fout.csv 2345 where test2.pl was my program, file3 was my input file, fout.csv is the file to be written and 2345 the shopID.

I used your input file and got the results:

DeviceID,PosTxnID,MobileNumber,Timestamp,PosID,PosUserID,ShopID,Prodco +de,ProdDescription,ProdDept,ProdGroup1,ProdGroup2,Qty,Value VINT01,112681,,2018/01/23 00:00,,,2345,ZB101,Acme Widget Large 20 inch +,,,,1,23.5 RR01,112683,,2018/01/23 00:00,,,2345,ZB101,Acme Widget Large 20 inch,, +,,2,47 VINT01,112681,,2018/01/23 00:00,,,2345,ZB1201,Acme Widget Small 5 inch +,,,,2,100 PIP01,112670,,2018/01/23 00:00,,,2345,ZB2101,Acme Widget Large 15 inch +,,,,1,12.5 VINT01,112681,,2018/01/23 00:00,,,2345,ZB2101,Acme Widget Large 15 inc +h,,,,1,12.5 VCR01,112674,,2018/01/23 00:00,,,2345,ZB2401,Acme Widget Medium 10 inc +h,,,,2,65 VINT01,112681,,2018/01/23 00:00,,,2345,ZB2501,Acme Widget Small 6 inch +,,,,1,37.5 CAPB01,112672,,2018/01/23 00:00,,,2345,ZB3501,Acme Widget GOLD,,,,1,29 SANML,112673,,2018/01/23 00:00,,,2345,ZB3501,Acme Widget GOLD,,,,1,29 RS01,112657,,2018/01/23 00:00,,,2345,ZB3701,Acme Widget Large 24 inch, +,,,1,21 CAPB01,112672,,2018/01/23 00:00,,,2345,ZB3701,Acme Widget Large 24 inc +h,,,,1,21 BPS01,112679,,2018/01/23 00:00,,,2345,ZB3701,Acme Widget Large 24 inch +,,,,2,42 VINT01,112681,,2018/01/23 00:00,,,2345,ZB3701,Acme Widget Large 24 inc +h,,,,1,21 MC01,112682,,2018/01/23 00:00,,,2345,ZB3701,Acme Widget Large 24 inch, +,,,1,21 PIP01,112670,,2018/01/23 00:00,,,2345,ZB3801,Regular Acme Widget Large + 20 inch,,,,1,21 RE01,112663,,2018/01/23 00:00,,,2345,ZB4001,High Type Acme Widget,,,,1 +,30 CAPB01,112672,,2018/01/23 00:00,,,2345,ZB4001,High Type Acme Widget,,, +,1,30 APP01,112685,,2018/01/23 00:00,,,2345,ZB4001,High Type Acme Widget,,,, +1,30 RR01,112683,,2018/01/23 00:00,,,2345,ZB401,Nugget Type Acme Widget,,,, +2,48 TB01,112665,,2018/01/23 00:00,,,2345,ZB4901,Acme Widget Large 17 inch, +,,,1,38.5 DD01,112659,,2018/01/23 00:00,,,2345,ZB501,Acme Widget hexangle,,,,3,9 +9 VINT01,112681,,2018/01/23 00:00,,,2345,ZB501,Acme Widget hexangle,,,,1 +,33 RS01,112657,,2018/01/23 00:00,,,2345,ZB701,Acme Widget Circular 20 inc +h,,,,1,33 PIP01,112670,,2018/01/23 00:00,,,2345,ZB701,Acme Widget Circular 20 in +ch,,,,1,33 BPS01,112679,,2018/01/23 00:00,,,2345,ZB701,Acme Widget Circular 20 in +ch,,,,1,33 RE01,112663,,2018/01/23 00:00,,,2345,ZB901,Acme Widget Square 3 inch,, +,,1,32 TB01,112665,,2018/01/23 00:00,,,2345,ZB901,Acme Widget Square 3 inch,, +,,1,32
The program first checks for 3 parameters on the input line (in @ARGV). It then gets the headers from the input file (to be used in a hash slice further down in the program @tmp{@hdrs} = split /,/;).

Then, it gets the columns needed to retrieve the data (also in another hash slice further down @tmp{@cols}).




  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2024-04-16 17:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found