Solved

Perl Script

Posted on 2009-07-15
7
185 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

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…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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 Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

803 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