Perl help

mmej

Junior Member
Jun 21, 2012
3
0
0
Hi

I wonder if anyone can help me here with perl as I'm only good in shell scriptting and i've been asked to do something in perl.

Basically i need to monitos a specific directory permanently. And once the files are delivered I have to generate a log and check if it is a duplicate and then forward it to an output system acording to the file type.

Here's an example:
FILE: MVNO_XXXXXX_MMS_PREPAID_Batch_id_FileSeqNum_YYYYMMDDhhmmss
OUTPUT SYSTEM: FRAUD & MDS
FILE: MVNO_UNB_XXXXXX_BatchID_SwitchId_YYYYMMDDhhmmss
OUTPUT SYSTEM: MDS

I know it's not an easy thing but in sh it would be simpler for me.

thanks anyway
 

Cogman

Lifer
Sep 19, 2000
10,286
147
106
If you really, REALLY, wanted to. Perl scripts can pretty much act exactly like a sh script. using back ticks (`) you execute a shell command. For example

my $frank = `cat bob`;

would put whatever is in bob into frank.

I wouldn't recommend doing this for all of your code, but it could be useful to try and get yourself better situated.

If you have specific questions, you'll get a better response. We won't write the script for you but we will answer questions to problems that you have.
 

mmej

Junior Member
Jun 21, 2012
3
0
0
Ok fair enough, i wasn't expecting you to do my job-:)

would you let me know if there's a way of getting the first and the last records of a file, ommiting the header and trailer?

thanks
 

Charles Kozierok

Elite Member
May 14, 2012
6,762
1
0
I could probably whip up an AWK script for you pretty fast. Just not sure exactly what you need it to do, please clarify?
 

Fallen Kell

Diamond Member
Oct 9, 1999
6,249
561
126
Well, if all you need to do is match one of those 2 patterns and check against previous files:

#!/usr/bin/perl

my $watch_dir = "/complete/path/to/watch/directory";

# This comes down to what you mean by duplicate
# This will check against all files it has previously processes
# with this script and will notify if it is a duplicate
my $previous_seen_files = "previous_files_list.txt";

open(PREV_FILES, ">>$previous_seen_files") or die("Could not open file: $previous_seen_files: $!");

opendir(WATCH, "$watch_dir") or die("Could not open directory: $watch_dir: $!");

my %previous_seen_files = load_previous_files($previous_seen_files);
while(my $file = readdir(WATCH)) {
process_file($file, \%previous_seen_files);
}

# Creates a hash table of files previously seen by your script
sub load_previous_files {
my $filename = shift;
my %ret;

open(IN, "$filename") or die("Could not open file: $filename: $!");
while(<IN>) {
$ret{chomp($_)} = 1;
}
close(IN);
return(%ret);
}

sub process_file {
my $file = shift;
chomp($file); #removes any newline character at the end
my $previous_seen_files_ptr = shift;

#Check that I have not seen it before
if (exists($previous_seen_files->{$file}) ) {
#Do something as I have seen this file before
}
else {
$previous_seen_files->{$file} = 1; # Updates the hash table saying that you have seen the filename
print PREV_FILES "$file\n"; # Appends the filename to the end of the previous_seen_files
if ($file =~ /^MVNO_UNB_.+_BatchID_SwitchId_\d{14}$/) {
#Output to MDS
}
elsif ($file =~ /^MVNO_.+_MMS_PREPAID_BatchID_FileSeqNum_\d{6}\s\d{8}$/) {
#Output to both MDS and Fraud
}
else {
#Unknown filename patern
}
}
 
Last edited:

mmej

Junior Member
Jun 21, 2012
3
0
0
thanks but in awk i could do it as well...but people here want it in perl...anyway i'm almost done thanks to al you're help

regards