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
Solved

Perl Script

Posted on 2009-07-15
7
186 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
7 Comments
 
LVL 40

Assisted Solution

by:mrjoltcola
mrjoltcola earned 150 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 250 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 100 total points
ID: 24863120
perl -lne 'BEGIN{$/="abcd\n"}print if $.>s/($).*//ms'
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Suggested Solutions

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…
On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
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…

809 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