Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 25 Jan 2000 17:41:17 -0600 (CST)
From:      Ryan Thompson <freebsd@sasknow.com>
To:        courtney@whtz.com
Cc:        questions@FreeBSD.ORG
Subject:   Re: Perl Script
Message-ID:  <Pine.BSF.4.10.10001251729320.36926-100000@sasknow.com>
In-Reply-To: <85256871.007E0786.00@mail.whtz.com>

next in thread | previous in thread | raw e-mail | index | archive | help
On Tue, 25 Jan 2000 courtney@whtz.com wrote:
> 
> hey everyone...
> 
> I am looking for some advice, or how to write a perl script that will open
> a file, and search it for a pre-determined string, and then output a few
> characters of that file to another file...
> 
> I.E. it opens file "a" and looks for the test "Bernie Courtney"
> 
> it then takes the next 4 characters after "Bernie Courtney" and writes them
> to file "B"
> 
> this seems like it could be done fairly simply....can anyone help me out
> with this...
> 
> Thanks in advance
> 
> Bernie Courtney

Sure, it is rather simple.  What you want to accomplish can be done with a
single regular expression, and the standard file IO fluff :-)

Since you said you want to create a script, include the following.  I have
NOT tested this, so please excuse any errors.

You may want to replace the input file "a" and output file "B" stuff with
STDIN/STDOUT, or make them command line arguments.  I'm just trying to
follow your message to the letter, here :-)

#!/usr/bin/perl

open INFILE, "<a";
my (@file) = <INFILE>;
close INFILE;

open OUTFILE, ">B";
select OUTFILE;

my $lines = join(/ /, @file);   # Join all lines together, including
				# newline characters

# Regular expression.  Match the string 'Bernie Courtney', with any four 
# characters following.  Print ONLY those four characters to the selected
# device, OUTFILE.  Case sensitive.  Use /ige instead of /ge for case
# insensitive matches.

$lines =~ s/Bernie\sCourtney(.{4})/print $1/ge;

close OUTFILE;

__END__;   # Done.


Hope this helps!

Virtually yours,
- Ryan

--
  Ryan Thompson <ryan@sasknow.com>	50% Owner, Sysadmin
  SaskNow Technologies     		http://www.sasknow.com
  #106-380 3120 8th St E   		Saskatoon, SK  S7H 0W2



To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?Pine.BSF.4.10.10001251729320.36926-100000>