LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-04-2004, 07:01 PM   #1
smaida
Member
 
Registered: Apr 2004
Location: Richmond, VA - USA
Distribution: Debian
Posts: 62

Rep: Reputation: 15
Perl Regex Help -- Reading In Text Files


Hi,

I am working on a perl script that needs to read in an entire text file and print out a certain section based on the input

Part of the text file is as follows--


Port 119 NNTP

* Microsoft Ports-Microsoft Exchange supports a News server running at this port.
* RFC977
* RFC1036

#-------------------------------------------

Port 120 CFDP

(UDP) Coherent File Distribution Protocol (CFDP)

* RFC1235

#-------------------------------------------

Port 123 NTP

(UDP) NTP (Network Time Protocol)

* RFC2030
* RFC1129

#--------------------------------------------



I need the script to read in and search the text file for a line "Port $port" where $port is the port number and then print out everyline until the "#-----" line.

Currently I have the following which searches the text file and finds the line "Port $port"... But where do I go from there?

sub port_definition
{
my $port = @_[0];

open(FILE,"ipac_port_defs") || die_nice("Can not open file \"ipac_port_defs\" for reading");
@port_file = <FILE>;
close(FILE);

foreach $line(@port_file){
if ($line =~ /^Port\s$port/){
$template{port_def} = $line;
}
}

print "Content-type: text/html\n\n"; # Print headers
parse("tpl/port_def");
}


If someone could just point me in the right direction.

Last edited by smaida; 04-04-2004 at 07:13 PM.
 
Old 04-05-2004, 12:27 AM   #2
meonkeys
Member
 
Registered: Apr 2004
Location: Minneapolis
Distribution: Ubuntu
Posts: 45

Rep: Reputation: 15
Code:
#!/usr/bin/perl -w
use IO::File;
use strict;

my ($input_file, $port) = @ARGV;
my $fh = IO::File->new("< $input_file") or die "open failed: $!";

my $sep = '#'.'-'x43;        # record separator
my $rex = qr/Port\s+$port/m; # regular expression

while (<$fh>) {
  print if (/$rex/ .. /$sep/ and !/$rex/ and !/$sep/);
}
Code:
# invoke like:   ./splitter.pl  <records>    <portNumber>
# for example:
$ ./splitter.pl  records.txt  119
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
regex Perl help igotlongestname Programming 2 09-14-2005 08:51 PM
perl - regex - negation beebop Programming 9 06-19-2005 09:25 PM
perl: using 'substr' in regex ananthbv Programming 5 11-03-2004 02:58 AM
RegEx Supported Text Editor - Perl Flavor germicide Linux - Software 1 10-05-2004 04:02 PM
Regex help needed in perl Dr Twox Programming 2 08-07-2004 06:58 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:30 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration