CongoGrey has asked for the wisdom of the Perl Monks concerning the following question:

I have a flat file that is large enough to fit into memory and all i want to do is read the file line by line and if the line contains a specific value 'read_community XXXXX' print 'read_community newstring'. Here is where I am at:
open NCNODE, "/path/nervecenter.node" or die "Error opening Nerve Center Node file ($!)"; foreach my $variable (<NCNODE>) { chomp; print STDOUT $variable; } close NCNODE;
I know that i need to place an if statement where i have the print line. But, i guess i am having an issue with the syntax. I was thinking that it would be something like:
if ($variable eq 'read_community'){ print; }
But, it does not seem to be correct. Below is a sample of the file.
begin node name Router on_off on group Frame_Relay suppress no auto_delete yes read_community readstring write_community readstring address 10.0.0.0 port 161 snmp_version 1 engineid 0 auth_protocol 0 is_key_ok 1 error_status 0 security_level 0 end node

Replies are listed 'Best First'.
Re: Simple Problem getting me Stumped
by KPeter0314 (Deacon) on Jul 01, 2003 at 17:07 UTC
    You could use something simple like the power of $_ and a while like this:
    open NCNODE, "/path/nervecenter.node" or die "Error opening Nerve Center Node file ($!)"; while (<NCNODE>) { chomp; if (/read_community/) { print; } } close NCNODE;

    -Kurt

      Thanks KPeter that way worked like a champ. Thanks for your help.
Re: Simple Problem getting me Stumped
by dws (Chancellor) on Jul 01, 2003 at 17:08 UTC
    Since you're trying to match a leading substring, use a regular expression.
    if ( $variable =~ /^read_community/ ) { ...
    should do it.

    Note that there's one other problem in your code.

    chomp;
    should be
    chomp($variable);
Re: Simple Problem getting me Stumped
by ant9000 (Monk) on Jul 01, 2003 at 17:09 UTC
    A one liner doing that:
    perl -pe's/^(read_community)\s+XXXXXX/\1 newstring/'