Solved

Explain What this script does

Posted on 2011-02-24
18
235 Views
Last Modified: 2012-05-11
Can you please explain what each line of code does to a CSV files.



print "Begin the program: \n";
open(IN,"$file") or die "can not open the inputfile\n";

my @lines = <IN>;

close IN;

for (@lines)
{
/^A(\d+)/ || next;
my $f="c:/Program Files/TIE Commerce/Crunchyroll/in_csv/$1.CRUsinglepo";
open OUT,">>$f" or die $!;
print OUT;
close OUT;
}
0
Comment
Question by:Navarre_EDI
  • 10
  • 8
18 Comments
 

Author Comment

by:Navarre_EDI
Comment Utility
These two line are important

for (@lines)
{
/^A(\d+)/ || next;
0
 
LVL 16

Expert Comment

by:sjklein42
Comment Utility
# loop through all the lines in the file, one by one

for (@lines)
{

# for each line that starts with an upper-case "A" followed by a number with one or more digits 0-9...

/^A(\d+)/ || next;

# ... build a string of a file name, substituting the number from the previous step as "$1"

my $f="c:/Program Files/TIE Commerce/Crunchyroll/in_csv/$1.CRUsinglepo";

# open the file whose name we just constructed, echo the contents of the file, and close it

open OUT,">>$f" or die $!;
print OUT;
close OUT;
}
0
 

Author Comment

by:Navarre_EDI
Comment Utility
So with the example below and you explain it to me I would have 4 files and 1 with multplie line in there correct?

A661025004,,2011-02-16 06:50:13,,,Michael McFarland,22 Foxtail Lane,,,,Trabuco Canyon,CA,92679,US,(949) 633-7436,,,,,,daily-deal-fma-season2-dvd-part1,,8001329,FULLMETAL ALCHEMIST - SEASON 2 - PART 1,1,EA,16.99,
A756435108,,2011-02-16 06:18:35,,,Michael Camacho,6501 Overheart Ln,,,,Columbia,MD,21045,US,(301) 524-3757,,,,,,daily-deal-fma-season2-dvd-part1,,8001329,FULLMETAL ALCHEMIST - SEASON 2 - PART 1,1,EA,16.99,
A000063477,,2011-02-16 06:17:00,,,Robert A. Guffin,Greens Farms Academy,35 Beachside Avenue,,,Greens Farms,CT,06838-0998,US,(203) 664-0785,,,,,,daily-deal-fma-season2-dvd-part1,,8001329,FULLMETAL ALCHEMIST - SEASON 2 - PART 1,1,EA,18.99,
A961008927,,2011-02-16 00:41:32,,,Reed Nakamura,2232 Hoonanea Street,(House In Back),,,Honolulu,HI,96822,US,(808) 344-3096,,,,,,daily-deal-fma-season2-dvd-part1,,8001329,FULLMETAL ALCHEMIST - SEASON 2 - PART 1,1,EA,18.99,
A961008927,,2011-02-16 00:41:32,,,Reed Nakamura,2232 Hoonanea Street,(House In Back),,,Honolulu,HI,96822,US,(808) 344-3096,,,,,,daily-deal-fma-season2-dvd-part1,,8001329,FULLMETAL ALCHEMIST - SEASON 2 - PART 1,1,EA,18.99,
0
 
LVL 16

Expert Comment

by:sjklein42
Comment Utility
First, be sure there are no files already in the output folder when the program starts running.  New data gets appended to the end of the files, so if there are files left over from a previous run they will only get biggger - not reset.

That aside, yes, the data you show should create four output files, one with two lines.
0
 

Author Comment

by:Navarre_EDI
Comment Utility
So what happen if I dont have a key number to build the files. Is there another way to build the files?

Example Below and they have CR or LF but the last two will need to be in the same file.

A661025004,,2011-02-16 06:50:13,,,Michael McFarland,22 Foxtail Lane,,,,
B108,,2011-02-16 06:18:35,,,Michael Camacho,6501 Overheart Ln,,,,
123,,2011-02-16 06:17:00,,,Robert A. Guffin,Greens Farms Academy,35 Beachside Avenue,,,,
09727,,2011-02-16 00:41:32,,,Reed Nakamura,2232 Hoonanea Street,(House In Back),,,,
09727,,2011-02-16 00:41:32,,,Reed Nakamura,2232 Hoonanea Street,(House In Back),,,,
0
 

Author Comment

by:Navarre_EDI
Comment Utility
On your presviuos respond that would only happen appending if the PO number was the same
0
 
LVL 16

Expert Comment

by:sjklein42
Comment Utility
With the code you posted in your question, only keys that start with "A" are recognized.  That is easy to change if it is not the way you want it to work.  Do you want to create a file for every different key?  Why do you need to strip off the "A" at the start of the key?  


In you question above, yes, only the last two lines would be in the same file.  Only keys that match will go into the same file.

If it helps, this version will create a file for every key, not just the ones starting with "A".

print "Begin the program: \n"; 
open(IN,"$file") or die "can not open the inputfile\n"; 

my @lines = <IN>;

close IN;

for (@lines)
{
/^([^\,]+)/ || next;
my $f="c:/Program Files/TIE Commerce/Crunchyroll/in_csv/$1.CRUsinglepo";
open OUT,">>$f" or die $!; 
print OUT; 
close OUT;
} 

Open in new window

0
 

Author Comment

by:Navarre_EDI
Comment Utility
Hello

I am looking and try to run but getting an error

C:\Perl\script>Audyssey_splitter.pl
Begin the program:
Invalid argument at C:\Perl\script\Audyssey_splitter.pl line 22.


 
#!/usr/bin/perl5


use strict;
my ($s, $mi, $h, $d, $m, $y) = (localtime())[0..5];
my $stamp = sprintf("%02d%02d%4d%02d%02d%02d", $m+1,$d,$y+1900,$h,$mi,$s);
my $file="Audyssey_Order_Req_02222011_121608.csv";
#my $file=$ARGV[0];

print "Begin the program: \n";
open(IN,"$file") or die "can not open the inputfile\n";

my @lines = <IN>;

close IN;

for (@lines)
{
/^([^\,]+)/ || next;
my $f="c:/Program Files/TIE Commerce/AUDYSSEY/in_csv/$1.AUDsinglepo";
open OUT,">>$f" or die $!;
print OUT;
close OUT;
}

#rename "$file", "d:/tiecom~1/eVision/AUDYSSEY/processed/old_$file${stamp}"
0
 
LVL 16

Expert Comment

by:sjklein42
Comment Utility
In Windows, use back-slashes for file paths,  They need to be doubled (quoted) when within a string with double-quotes - like this:

my $f="c:\\Program Files\\TIE Commerce\\AUDYSSEY\\in_csv\\$1.AUDsinglepo";

Open in new window

0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Navarre_EDI
Comment Utility
I am still getting the same error at after I made the change.

open OUT,">>$f" or die $!;


Also how about if the data file looks like this and I dont want the first line which is just headers.

PONUMBER,ORDERREFID,ORDERDATE,REQUESTEDDELIVERYDATE,ORDERCANCELDATE,SHIPPING_NAME,SHIPPING_STREET1,SHIPPING_STREET2,
A661025004,,2011-02-16 06:50:13,,,Michael McFarland,22 Foxtail Lane,,,,
B108,,2011-02-16 06:18:35,,,Michael Camacho,6501 Overheart Ln,,,,
123,,2011-02-16 06:17:00,,,Robert A. Guffin,Greens Farms Academy,35 Beachside Avenue,,,,
09727,,2011-02-16 00:41:32,,,Reed Nakamura,2232 Hoonanea Street,(House In Back),,,,
09727,,2011-02-16 00:41:32,,,Reed Nakamura,2232 Hoonanea Street,(House In Back),,,,


0
 
LVL 16

Expert Comment

by:sjklein42
Comment Utility
I have changed it to ignore the header line, and it also will show debugging output when it opens the output files.

Be sure the output folder exists before running, or it will fail:

c:\Program Files\TIE Commerce\AUDYSSEY\in_csv\

Please try it and post the results.

#!/usr/bin/perl5 


use strict;
my ($s, $mi, $h, $d, $m, $y) = (localtime())[0..5];
my $stamp = sprintf("%02d%02d%4d%02d%02d%02d", $m+1,$d,$y+1900,$h,$mi,$s);
my $file="Audyssey_Order_Req_02222011_121608.csv";
#my $file=$ARGV[0];

print "Begin the program: \n"; 
open(IN,"$file") or die "can not open the inputfile\n"; 

$headerLine = <IN>;	# ignore header line
my @lines = <IN>;

close IN;

for (@lines)
{
/^([^\,]+)/ || next;

my $f="c:\\Program Files\\TIE Commerce\\AUDYSSEY\\in_csv\\$1.AUDsinglepo";
print STDERR "->->-> opening file $f\n";

open OUT,">>$f" or die $!; 
print OUT; 
close OUT;
}

#rename "$file", "d:/tiecom~1/eVision/AUDYSSEY/processed/old_$file${stamp}" 

Open in new window

0
 

Author Comment

by:Navarre_EDI
Comment Utility
Hello

Here is the error I got.

I am send you the data files.

C:\Perl\script>mikea.pl
Global symbol "$headerLine" requires explicit package name at C:\Perl\script\mik
ea.pl line 13.
Execution of C:\Perl\script\mikea.pl aborted due to compilation errors.
temp2.csv
0
 
LVL 16

Expert Comment

by:sjklein42
Comment Utility
Sorry, forgot you were running with "strict" PERL syntax.  Try this:

#!/usr/bin/perl5 


use strict;
my ($s, $mi, $h, $d, $m, $y) = (localtime())[0..5];
my $stamp = sprintf("%02d%02d%4d%02d%02d%02d", $m+1,$d,$y+1900,$h,$mi,$s);
my $file="Audyssey_Order_Req_02222011_121608.csv";
#my $file=$ARGV[0];

print "Begin the program: \n"; 
open(IN,"$file") or die "can not open the inputfile\n"; 

my $headerLine = <IN>;	# ignore header line
my @lines = <IN>;

close IN;

for (@lines)
{
/^([^\,]+)/ || next;

my $f="c:\\Program Files\\TIE Commerce\\AUDYSSEY\\in_csv\\$1.AUDsinglepo";
print STDERR "->->-> opening file $f\n";

open OUT,">>$f" or die $!; 
print OUT; 
close OUT;
}

#rename "$file", "d:/tiecom~1/eVision/AUDYSSEY/processed/old_$file${stamp}" 

Open in new window

0
 

Author Comment

by:Navarre_EDI
Comment Utility
Hello

Still get a problem at line open OUT,">>$f" or die $!;
but it looks like the output is correct.

C:\Perl\script>mikea.pl
Begin the program:
->->-> opening file c:\Program Files\TIE Commerce\AUDYSSEY\in_csv\47956856.AUDsi
nglepo
->->-> opening file c:\Program Files\TIE Commerce\AUDYSSEY\in_csv\ABC.AUDsinglep
o
->->-> opening file c:\Program Files\TIE Commerce\AUDYSSEY\in_csv\CFD.AUDsinglep
o
->->-> opening file c:\Program Files\TIE Commerce\AUDYSSEY\in_csv\111.AUDsinglep
o
->->-> opening file c:\Program Files\TIE Commerce\AUDYSSEY\in_csv\111.AUDsinglep
o
->->-> opening file c:\Program Files\TIE Commerce\AUDYSSEY\in_csv\
.AUDsinglepo
Invalid argument at C:\Perl\script\mikea.pl line 24.
0
 

Author Comment

by:Navarre_EDI
Comment Utility
I think my format is incorrect in the layoput of the flat file
0
 
LVL 16

Accepted Solution

by:
sjklein42 earned 500 total points
Comment Utility
I made a change so it will ignore blank lines

#!/usr/bin/perl5 


use strict;
my ($s, $mi, $h, $d, $m, $y) = (localtime())[0..5];
my $stamp = sprintf("%02d%02d%4d%02d%02d%02d", $m+1,$d,$y+1900,$h,$mi,$s);
my $file="Audyssey_Order_Req_02222011_121608.csv";
#my $file=$ARGV[0];

print "Begin the program: \n"; 
open(IN,"$file") or die "can not open the inputfile\n"; 

my $headerLine = <IN>;	# ignore header line
my @lines = <IN>;

close IN;

for (@lines)
{
s/[\r\n]//g;
/^([^\,]+)/ || next;

my $f="c:\\Program Files\\TIE Commerce\\AUDYSSEY\\in_csv\\$1.AUDsinglepo";
print STDERR "->->-> opening file $f\n";

open OUT,">>$f" or die $!; 
print OUT; 
close OUT;
}

#rename "$file", "d:/tiecom~1/eVision/AUDYSSEY/processed/old_$file${stamp}" 

Open in new window

0
 

Author Comment

by:Navarre_EDI
Comment Utility
When ran this script it work but with the sample below it only add one line for 86665 when there should have been two


PONUMBER,ORDERREFID,ORDERDATE,REQUESTEDDELIVERYDATE,ORDERCANCELDATE,SHIPPING_NAME,SHIPPING_STREET1,SHIPPING_STREET2,SHIPPING_APTSUITEPOBOX,SHIPPING_ATTN,SHIPPING_CITY,SHIPPING_STATEREGION,SHIPPING_POSTALCODE,SHIPPING_COUNTRY,SHIPPING_CONTACTPHONE,SHIPPING_EMAILADDRESS,SHIPPINGMETHOD,SHIPPINGCARRIER,LINENUMBER,LINEITEMREFID,CUSTOMERITEMNUMBER,UPCNUMBER,NAVARREITEMNUMBER,SHORTDESCRIPTION,QUANTITY,UOM,UNITPRICE,LINEITEMPRICE
86665,1045,2/11/2011,2/18/2011,,Deborah D'Arcy,55 Liberty Street,,#9B,,New York,NY,10005,US,212-555-5555,nycdarcy@yahoo.com,UPS Ground,,1,,AUD010002000102,853041002027,8064053,SOMA Dock,1,EA,399,399
86665,1045,2/11/2011,2/18/2011,,Deborah D'Arcy,55 Liberty Street,,#9B,,New York,NY,10005,US,212-555-5555,nycdarcy@yahoo.com,UPS Ground,,1,,AUD010002000102,853041002027,8064053,SOMA Dock,1,EA,399,399
4665,1045,2/11/2011,2/18/2011,,Deborah D'Arcy,55 Liberty Street,,#9B,,New York,NY,10005,US,212-555-5555,nycdarcy@yahoo.com,UPS Ground,,1,,AUD010002000102,853041002027,8064053,SOMA Dock,1,EA,399,399
665,1045,2/11/2011,2/18/2011,,Deborah D'Arcy,55 Liberty Street,,#9B,,New York,NY,10005,US,212-555-5555,nycdarcy@yahoo.com,UPS Ground,,1,,AUD010002000102,853041002027,8064053,SOMA Dock,1,EA,399,399
Aqwqq,1045,2/11/2011,2/18/2011,,Deborah D'Arcy,55 Liberty Street,,#9B,,New York,NY,10005,US,212-555-5555,nycdarcy@yahoo.com,UPS Ground,,1,,AUD010002000102,853041002027,8064053,SOMA Dock,1,EA,399,399
0
 
LVL 16

Expert Comment

by:sjklein42
Comment Utility
Works for me (naturally).  :)

Please post the "logging" output you get when you run the program.  Do you get two "86665" lines like I did?  If you made any changes at all to the code, please post the exact PERL file source code as it stands now.

c:\temp>perl foo.pl foo.dat
Begin the program:
->->-> opening file c:\temp\86665.AUDsinglepo
->->-> opening file c:\temp\86665.AUDsinglepo
->->-> opening file c:\temp\4665.AUDsinglepo
->->-> opening file c:\temp\665.AUDsinglepo
->->-> opening file c:\temp\Aqwqq.AUDsinglepo

c:\temp>type 86665.AUDsinglepo
86665,1045,2/11/2011,2/18/2011,,Deborah D'Arcy,55 Liberty Street,,#9B,,New York,NY,10005,US,212-555-5555,nycdarcy@yahoo.com,UPS Ground,,1,,AUD010002000102,85304
1002027,8064053,SOMA Dock,1,EA,399,399
86665,1045,2/11/2011,2/18/2011,,Deborah D'Arcy,55 Liberty Street,,#9B,,New York,NY,10005,US,212-555-5555,nycdarcy@yahoo.com,UPS Ground,,1,,AUD010002000102,85304
1002027,8064053,SOMA Dock,1,EA,399,399

c:\temp>

Open in new window

0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now