Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Need to print everything between 2 patterns.

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
jmgibson1981
Posts: 295
Joined: 2015-06-07 14:38
Has thanked: 11 times
Been thanked: 32 times

Need to print everything between 2 patterns.

#1 Post by jmgibson1981 »

This is as far as I've gotten. I need to print the lines between Pattern2 and the yet to me decided Pattern3. I'm not sure how to extract that from my input array. It was suggested I join the array first to a variable but I dont know where to go from there. Suggestion would be appreciated.

Code: Select all

#!/usr/bin/perl

use strict;
use warnings;

my @input = ();

# grab stdin and put in @input array

while (<>) {
	last if ($_ eq '');
	push (@input, $_);
}

foreach my $line (@input) {
	if ($line =~ /^Pattern1/) {
		print "$line\n";
	}
	elsif ($line =~ /^Pattern2/) {
#		print "$line\n";
		my $string = join ("", @input);
		}
	}
}


jmgibson1981
Posts: 295
Joined: 2015-06-07 14:38
Has thanked: 11 times
Been thanked: 32 times

Re: Need to print everything between 2 patterns.

#3 Post by jmgibson1981 »

Actually real work. Not school. I just can't figure the syntax. They want me to learn perl for job and gave me the camel book but it's a ton of fluff, hard to find what I'm looking for.

User avatar
GarryRicketson
Posts: 5644
Joined: 2015-01-20 22:16
Location: Durango, Mexico

Re: Need to print everything between 2 patterns.

#4 Post by GarryRicketson »

I don't know that much about Perl, I kind of just started myself,
To start though you need to understand and be able to do some basic
input,...
There are a lot of good tutorials, available,... , also "PerlMonks", is a pretty
good site to get some help,..you can find it by doing a search.
You are getting paid, to do this, I am not,... nobody is going to write the
script for you, here is something to help
you get started though:
Example:

Code: Select all

print "Please enter your name:\n";
$name = <>;
chomp($name);
print "Hello, ", $name, "!\n";
This is a pretty good tutorial, I found it very help full,..
http://perl-begin.org/tutorials/perl-for-newbies/part1/

Need to print everything between 2 patterns using Perl
Seems to get a lot of results,..
If I was getting paid to do this, I would look at all the results, and then start trying
some simple scripts, modify them to do do exactly what I want, ...that is what I do
for my own projects as well, ...Perl is not that difficult to start understanding, you
probably can figure it out, with a day or 2 of serious studying.

Post Reply