Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

wildcards 1

Status
Not open for further replies.

sauser

Programmer
Aug 22, 2001
49
US
Hey Guys,

One of the guys in my school asked me to help to organize his file for his business with credit card numbers and experation dates. It is a really big file with creadit card numbers and expiration dates. What i wanted to do is to devide this big file onto two smaller ones one of them will keep only exp dates the otehr one credit card numbers. But I have been facing this problem. When i try to write exp date into anotehr text file the only part that copies is exp date and nothing else. The actual date does not go to the file. The line i wrote was like that

Print filename "Exp date $c\n"
I am new to perl any help would be appreciated.

Thanks
 
If you can show a sample of how the data(Card Nos and Dates)is stored in your file and the code which writes the data into two different files,we can home into the problem.

Reg. the code snippet you have shown:

1. 'perldoc -f Print' yields
"No documentation for perl function `Print' found "

2. filename should be replaced by your Filehandle.



CM
 
ok the info goes liek this let me give you an exemple

action flag = P

tran type = 5

entry method = 0

receipt number = 1003

card data = 1111111111111111

exp date = 0204

amount = 3998

street for AVS = 5

zip code for AVS = 98118

and so on it is a really big text file. So now i need the script to go through the file and select all exp data and throuw it into a file and then go again but this time throw all credit card numbers into a different file. I have been wodering how to do it the better way.

Thanks

 
Try the following code if the data format is consistent throughout in the file.

HTH



#!/usr/bin/perl

# File containing Card Data
open(DATA,"data" ) or die "File Open Error\n"

# File that'll contain dates
open(DATES,">dates") or die "File Open Error\n"


# File that'll contain Card Numbers
open(NUMS,">cardnos") or die "File Open Error\n"

while(<DATA>)
{
if (/exp date/)
{
($junk,$date)=split(/=/);
print DATES &quot;$date\n&quot;;
}


if(/card data/)
{

($junk,$num)=split(/=/);
print NUMS &quot;$num\n&quot;;
}


}

close DATA;
close DATES;
close NUMS;
 
hey thanks a lot. I just have a question what does if (/exp date/)means? what kinda difference those slashes make?

Thanks
 
What i don't understand is what is happening inside If statement.
 
The line &quot;if (/exp date/)&quot; is shorthand for:
if ( $_ =~ m/exp date/)
For each iteration of the while loop, the variable &quot;$_&quot; is set to the contents of a line in the file (including the new line character). The 'm//' is the pattern matching operator. This line says: if &quot;$_&quot; contains the pattern &quot;exp date&quot;, execute the following block of code.

By the way, I would add a 'chomp($_);' at the beginning of the while loop to remove the new line character. If you don't, the $date and $num variables will have a new line character at the end.

 
Oops ...I missed the chomp .
Thanx for the reminder ,raider2001

[ From next time on ..Im gonna test the code before putting it in ]

C


 
hey guys,

thanks a lot

But i have one more question.

Let's say i wanted all the info to be printed in one file with a specific format. Like let's say

$expdate,$creditcardnumber,

so everything using comma
would this work

$_=~/(...),(...)/
$1=$expdare
$2=$creditcardnumber

and then

print FILENAME $_\n

i was just wondering
 
You can do the formatting with the print statement like this:

print FILEHANDLE &quot;$expdate,$creditcardnumber,\n&quot;;

 
thanks a lot guys.
Perl is the first srcipting language I ever learned. And I am getting really surprised about in how many ways you can do things!

Thanks for shorthand raider!

 
i have mysql installed on windows 2000. COuld somebody suggest a way to add the resulting text file into mysql database.


Thanks
 
I was just wondering how would you use textfile as a source for insert SQL query.
 
For what it's worth. If you are parsing a really large file, it would be more effecient to modify latch's script to look like this:

#!/usr/bin/perl

# File containing Card Data
open(DATA,&quot;data&quot; ) or die &quot;File Open Error\n&quot;

# File that'll contain dates
open(DATES,&quot;>dates&quot;) or die &quot;File Open Error\n&quot;


# File that'll contain Card Numbers
open(NUMS,&quot;>cardnos&quot;) or die &quot;File Open Error\n&quot;

while(<DATA>)
{
chomp;
# an anchor makes a regex run MUCH faster!
if (/^exp date/)
{
# Slightly faster assignment without creating $junk
$date=(split('='))[1];
print DATES &quot;$date\n&quot;;
}


if(/^card data/)
{

$num=(split('='))[1];
print NUMS &quot;$num\n&quot;;
}


}

close DATA;
close DATES;
close NUMS;

If you have the RAM to throw at the problem, you should also build the output files in memory and write them out all at once. This reduced the discrete system I/O calls and allows the disk subsystem to run a little faster.

But then again, these changes would only be noticable if you are dealing with a LARGE amount of data, at least 20-30 Gb or so.
 
>If you have the RAM to throw at the problem, you should >also build the output files in memory and write them out >all at once. This reduced the discrete system I/O calls >and allows the disk subsystem to run a little faster.


Request a clarification here....does a system I/O call get invoked evrytime a 'print <FILEHANDLE> ' occurs or is it just that data is dumped into the Kernel bufffer and later gets committed onto the disk?


 
The output from 'print' will be buffered unless $| is nonzero.
'print <HANDLE>' will send it's output to the buffer. The buffer will flush as needed. A disk I/O will occur upon flush. If you set $| = 1, then, it will flush with every print or write and you will do a disk hit every time.

HTH Please use descriptive titles and check the FAQs.
And, beware the evil typo.
 
($junk,$date)=split(/=/);


could somebody explain what is happening in this line.

Thanks
 
hey guys,

I can't make it working!!

I can;t make it so the result goes into one line

like that

exp data, credit card number

because the text file has lots of junk

please help!
 
The date and number matching operators work if the lines containing the sought values *begin* with 'exp date' and 'card data' if you go by esserc's script. As mentioned in one of the replies, all the posted scripts work only if the data format is *consistent* throughout the file.

Although the following has slightly better chances of seeking what you are looking for, there is no guarantee that this might work as you need to list out the ways by which the lines containing the data could posssibly appear and work on a perfect macthing expression.


# This matches a line containing 'date', split the
# matched line based on '=' (I assume a single '=' is
# present in the line ) and then matches the latter
# half and in it, matches a string of numbers which, hopefully is what u want.


if(/date/
{
(split(/=/))[1]=~/(\d+)/;
$date=$1;

}

# Likewise for number





HTH
C



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top