Solved

Perl Script

Posted on 2009-07-15
7
183 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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 …
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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 explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

932 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

12 Experts available now in Live!

Get 1:1 Help Now