Perl Script

Hi guys

i trying to write an perl sript to eliminate the particular string(date and time ) in the record in the file .the string(date and time) is occuring at begining of eacth record

i have file which has 20 records starting with date and time followeb by message i need an perl script to remove date and time  before every message

2009-07-13 14:43:38.450 XYZ+blablabalbalbalablabalbal.....................................
2009-07-13 14:43:38.450 XYZ+blablabalbalbalablabalbal.....................................
2009-07-13 14:43:38.450 XYZ+blablabalbalbalablabalbal.....................................
2009-07-13 14:43:38.450 XYZ+blablabalbalbalablabalbal.....................................
2009-07-13 14:43:38.450 XYZ+blablabalbalbalablabalbal.....................................
2009-07-13 14:43:38.450 XYZ+blablabalbalbalablabalbal.....................................
2009-07-13 14:43:38.450 XYZ+blablabalbalbalablabalbal.....................................
2009-07-13 14:43:38.450 XYZ+blablabalbalbalablabalbal.....................................
2009-07-13 14:43:38.450 XYZ+blablabalbalbalablabalbal.....................................
2009-07-13 14:43:38.450 XYZ+blablabalbalbalablabalbal.....................................
2009-07-13 14:43:38.450 XYZ+blablabalbalbalablabalbal.....................................
2009-07-13 14:43:38.450 XYZ+blablabalbalbalablabalbal.....................................
2009-07-13 14:43:38.450 XYZ+blablabalbalbalablabalbal.....................................
2009-07-13 14:43:38.450 XYZ+blablabalbalbalablabalbal.....................................
2009-07-13 14:43:38.526 XYZ+blablabalbalbalablabalbal.....................................
2009-07-13 14:43:38.534 XYZ+blablabalbalbalablabalbal.....................................
2009-07-13 14:43:38.543 XYZ+blablabalbalbalablabalbal.....................................
2009-07-13 14:43:38.544 XYZ+blablabalbalbalablabalbal.....................................
2009-07-13 14:43:38.550 XYZ+blablabalbalbalablabalbal.....................................
2009-07-13 14:43:38.600 XYZ+blablabalbalbalablabalbal.....................................

every message starts with XYZ+ .

date and time varies.

the out put file shd have  
XYZ+blablabalbalbalablabalbal.....................................
XYZ+blablabalbalbalablabalbal.....................................
XYZ+blablabalbalbalablabalbal.....................................
XYZ+blablabalbalbalablabalbal.....................................
XYZ+blablabalbalbalablabalbal.....................................
XYZ+blablabalbalbalablabalbal.....................................
XYZ+blablabalbalbalablabalbal.....................................
XYZ+blablabalbalbalablabalbal.....................................
XYZ+blablabalbalbalablabalbal.....................................
XYZ+blablabalbalbalablabalbal.....................................
XYZ+blablabalbalbalablabalbal.....................................
XYZ+blablabalbalbalablabalbal.....................................
XYZ+blablabalbalbalablabalbal.....................................
XYZ+blablabalbalbalablabalbal.....................................
XYZ+blablabalbalbalablabalbal.....................................
XYZ+blablabalbalbalablabalbal.....................................
XYZ+blablabalbalbalablabalbal.....................................
XYZ+blablabalbalbalablabalbal.....................................
XYZ+blablabalbalbalablabalbal.....................................
XYZ+blablabalbalbalablabalbal.....................................

please help me.
mannnAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Carl BohmanCommented:
perl -pe 's/^.*?(?=XYZ\+)//' infile.txt >outfile.txt
0
zlobchoCommented:

#!/usr/bin/perl
 
open (IN,"data.txt") or die;
open (OUT,">dataout.txt") or die;
# print $time();
foreach (<IN>){
$_=~/(\d*\-\d*\-\d*\s*\d*\:\d*\:\d*\.\d*\s*)(.*)/;
print $2."\n";
print OUT $2."\n";
}
close IN;
close OUT;

Open in new window

0
flob9Commented:
If you dont know XYZ :

#!/usr/bin/perl
while(<STDIN>)
{
    my($line) = $_;
    $line =~ s/^([0-9: \-\.)]+)//g;
    print($line);
}

or

perl -pe 's/^([0-9: \-\.)]+)//g' file.txt


0
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Adam314Commented:

##### To create a new file
perl -pe 's/^\d+-\d+-\d+ \d+:\d+:\d+\.\d+ //' input.txt > output.txt
 
##### To modify your existing file, creating a backup
perl -ibak -pe 's/^\d+-\d+-\d+ \d+:\d+:\d+\.\d+ //' input.txt

Open in new window

0
mannnAuthor Commented:
thanks guys


i wanna include this operation in the existing perl script....


their are many files like this

my actual task is i need an script forread an files from the dir one file at time and apply above one to the each file the out put can be a same file or new file in that dir or new dir


0
flob9Commented:
you should use adam314 2nd solution with *.txt
0
Carl BohmanCommented:

my $Dir = '/your/path';
 
opendir(DIR, $Dir) or die $!;
 
while(my $FileName = readdir(DIR))
{
 if (open(INFILE, "$Dir/$FileName"))
 {
  if (open(OUTFILE, ">$Dir/$FileName.out"))
  {
   while(<INFILE>)
   {
    s/^.*?(?=XYZ\+)//;
    print OUTFILE $_;
   }
 
   close(OUTFILE);
  } else
  {
   warn $!;
  }
 
  close(INFILE);
 } else
 {
  warn $!;
 }
}
 
closedir(DIR);

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mannnAuthor Commented:
how can i use that command in perl script file .

the input file is an intermidiate output file in the perl script ineed to apply above one said by adam314 in script file

0
Carl BohmanCommented:
You could use backticks or the system function to call it.
0
Adam314Commented:
Being an intermediate file that needs to be processed - do you want to write the intermediate file with the date/time, then read it, remove the date/time, and write it - or would you rather just write it without the date/time in the first place?

If you'd rather just not write the date/time, post the part of your code that writes the intermediate file, and we can help you modify that so the date/time is not written.


If you want to keep writing the file as is, and just have another scrip to modify it, you can call the code I gave like this:
##### replace input.txt with the file name
#unix:    system("perl -ibak -pe 's/^\d+-\d+-\d+ \d+:\d+:\d+\.\d+ //' input.txt");
#windows: system('perl -ibak -pe "s/^\d+-\d+-\d+ \d+:\d+:\d+\.\d+ //" input.txt');

Open in new window

0
mannnAuthor Commented:
thanks adam314


here is the code which i iam using to get the next record (requests) if it matches the Regular Request Message in  record

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;


open(my $in, "<SAevent.txt") or die "Could not open input: $!\n";

open(my $out, ">SAEOUT.txt") or die "Could not open output: $!\n";

my $keep;
while(<$in>){
        if($keep){

print $out $_;
print $out "msgend\n";
$keep=0;
}
elsif(/Regular Request Message/) {$keep = 1;}
  }

After the above script completes it will out put the record which  contain the date and time and every message starts with XYZ as mentioned above so i need the only message .

and can you modify my script to read the files from dir (since i got many files )

please can you modify the script as we can give the dir at the runtime

i mean when we run the script

like
perl script.pl  /path/dir

thanks in advance
0
ozoCommented:

@ARGV=<$ARGV[0]/*>;

open(my $out, ">SAEOUT.txt") or die "Could not open output: $!\n";

my $keep;
while(<>){
        if($keep){
s/^\d+-\d+-\d+ \d+:\d+:\d+\.\d+ //;
print $out $_;
print $out "msgend\n";
$keep=0;
}
elsif(/Regular Request Message/) {$keep = 1;}
  }
0
mannnAuthor Commented:
thanks guys for the help ...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.