From owner-freebsd-questions Tue Jan 25 15:41:32 2000 Delivered-To: freebsd-questions@freebsd.org Received: from sasknow.com (h139-142-245-96.ss.fiberone.net [139.142.245.96]) by hub.freebsd.org (Postfix) with ESMTP id 6CFD114DD3 for ; Tue, 25 Jan 2000 15:40:40 -0800 (PST) (envelope-from freebsd@sasknow.com) Received: from localhost (freebsd@localhost) by sasknow.com (8.9.3/8.9.3) with ESMTP id RAA36995; Tue, 25 Jan 2000 17:41:17 -0600 (CST) (envelope-from freebsd@sasknow.com) Date: Tue, 25 Jan 2000 17:41:17 -0600 (CST) From: Ryan Thompson To: courtney@whtz.com Cc: questions@FreeBSD.ORG Subject: Re: Perl Script In-Reply-To: <85256871.007E0786.00@mail.whtz.com> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: owner-freebsd-questions@FreeBSD.ORG Precedence: bulk X-Loop: FreeBSD.ORG 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, "; 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 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