?
Solved

Perl Script

Posted on 2009-07-15
7
Medium Priority
?
189 Views
Last Modified: 2012-05-07
Hi guys iam trying to write an perl script which does the below

Please help me ,,,,,,,

if file contains 20 records

abcd
blablablablablablablablablabla...
efgh
blalabalablabla.....
abcd
blablablablablablablablablabla...
efgh
blalabalablabla.....
abcd
blablablablablablablablablabla...
efgh
blalabalablabla.....
abcd
blablablablablablablablablabla...
efgh
blalabalablabla.....
abcd
blablablablablablablablablabla...
efgh
blalabalablabla.....
...

I need the script which takes the line or record after the abcd  and writes in new file

new file should contain

blablablablablablablablablabla...
blablablablablablablablablabla...
blablablablablablablablablabla...
blablablablablablablablablabla...
blablablablablablablablablabla...

i need perl script which does this 
 
#!/usr/local/bin/perl
open (MYFILE, 'XYZ.txt');
 
open (OFILE, '>ABCT.txt');
while (<MYFILE>) {
if (string found )
print OFILE "$_"+1;/* I need the next record if the string mach */
}

Open in new window

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
7 Comments
 
LVL 40

Assisted Solution

by:mrjoltcola
mrjoltcola earned 600 total points
ID: 24862197
I'm thinking someting like this will work.

while (<MYFILE>) {
  if(/^abcd\n(.*?)\n/s) {
     print QFILE "$1\n";
  }
}
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 1000 total points
ID: 24862361

#!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
 
open(my $in, "<file1.txt") or die "Could not open input: $!\n";
open(my $out, ">file2.txt") or die "Could not open output: $!\n";
 
my $keep;
while(<$in>){
	if($keep) {print $out $_;$keep=0;}
	elsif(/abcd/) {$keep = 1;}
}

Open in new window

0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 400 total points
ID: 24863120
perl -lne 'BEGIN{$/="abcd\n"}print if $.>s/($).*//ms'
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 84

Expert Comment

by:ozo
ID: 24863535
perl -ne "print if (/^abcd\b/.../^/)==2"
0
 
LVL 9

Expert Comment

by:ghostdog74
ID: 24865905

perl -ne 'print if $c && $c--;$c=1 if /abcd/' file

Open in new window

0
 
LVL 84

Expert Comment

by:ozo
ID: 24866052
perl -ne 'print if $c;$c=/abcd/'  file
0
 

Author Comment

by:mannn
ID: 24869890
Thank you all of you, mrjoltcola,adam314,ozo and ghost dog74
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
Six Sigma Control Plans

765 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