Solved

Perl Script

Posted on 2009-07-15
7
182 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
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 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

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…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

762 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

20 Experts available now in Live!

Get 1:1 Help Now