Solved

Perl Script

Posted on 2009-07-16
13
252 Views
Last Modified: 2012-05-07
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.
0
Comment
Question by:mannn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +3
13 Comments
 
LVL 13

Expert Comment

by:Carl Bohman
ID: 24870181
perl -pe 's/^.*?(?=XYZ\+)//' infile.txt >outfile.txt
0
 
LVL 6

Assisted Solution

by:zlobcho
zlobcho earned 100 total points
ID: 24870348

#!/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
 
LVL 14

Expert Comment

by:flob9
ID: 24870382
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 39

Expert Comment

by:Adam314
ID: 24870506

##### 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
 

Author Comment

by:mannn
ID: 24870826
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
 
LVL 14

Expert Comment

by:flob9
ID: 24870901
you should use adam314 2nd solution with *.txt
0
 
LVL 13

Accepted Solution

by:
Carl Bohman earned 300 total points
ID: 24870949

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
 

Author Comment

by:mannn
ID: 24870969
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
 
LVL 13

Expert Comment

by:Carl Bohman
ID: 24871066
You could use backticks or the system function to call it.
0
 
LVL 39

Assisted Solution

by:Adam314
Adam314 earned 100 total points
ID: 24871121
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
 

Author Comment

by:mannn
ID: 24872060
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
 
LVL 84

Expert Comment

by:ozo
ID: 24872604

@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
 

Author Comment

by:mannn
ID: 24872944
thanks guys for the help ...
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Six Sigma Control Plans

705 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