Solved

Perl Script

Posted on 2009-07-16
13
225 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
  • 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
stftime format 4 51
Perl DBI Transactions Using Custom Module 7 41
Perl Frameworks 1 53
Perl Write to Specific line in a file 15 51
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…
Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

757 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

13 Experts available now in Live!

Get 1:1 Help Now