Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


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

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


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>;

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

print "Content-type: text/html\n\n"; # Print headers

If someone could just point me in the right direction.

Last edited by smaida; 04-04-2004 at 06:13 PM.
Old 04-04-2004, 11:27 PM   #2
Registered: Apr 2004
Location: Minneapolis
Distribution: Ubuntu
Posts: 45

Rep: Reputation: 15
#!/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/);
# invoke like:   ./  <records>    <portNumber>
# for example:
$ ./  records.txt  119


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 07:51 PM
perl - regex - negation beebop Programming 9 06-19-2005 08:25 PM
perl: using 'substr' in regex ananthbv Programming 5 11-03-2004 01:58 AM
RegEx Supported Text Editor - Perl Flavor germicide Linux - Software 1 10-05-2004 03:02 PM
Regex help needed in perl Dr Twox Programming 2 08-07-2004 05:58 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:50 AM.

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