Sreeja
Sreeja

Reputation: 151

Why this piece of code does not work in perl...Its simple

#!usr/bin/perl
$file_name = "file.txt";
open(FILE,$file_name);
while(<FILE>)
{
my $line = $_;
if($line =~ m/Svr\b/)
{
my $server_name;
$server_name  = $1;
print $server_name;
}
}

file.txt:

 ewrerfSvr//To be extracted
 Rate=rpm
 ID=123
 RATE=45
 ADDR=retriveBal
 Grocer="-r -e ${MAIN_ROOT}/logs/stderr -o ${MAIN_ROOT}/logs/stdout -A --"
freedonSvr
 BALFSvr   //to be extracted
 Rate=rpm1
 ID=12323
 RATE=45etf
 ADDR=retriveBal
 Grocer="-r -e ${MAIN_ROOT}/logs/stderr -o ${MAIN_ROOT}/logs/stdout -A --"
freedonSvr -D ${REV_AccountBalance_NAME}"//

Also I want to extract:

 REV_AccountBalance

Give me suggestion to do this using regular expression.

Upvotes: 0

Views: 130

Answers (2)

ysth
ysth

Reputation: 98398

$1 will get you the part of the matched string in capturing parentheses, but you don't have those. Did you mean your regex to be m/Svr\b(.+)/ ? Please show the output you are wanting to get; the comments in file.txt aren't explicit enough.

Upvotes: 2

a&#39;r
a&#39;r

Reputation: 36999

#!usr/bin/perl
use strict;
use warnings;

my $file_name = "file.txt";
open(my $fh,$file_name) or die "Could not open file";

while(<$fh>) {
    if (m/(\w*Svr)\b/) { print "$1\n"; }
}

You should get used to using warnings and strict and trapping errors from calls like open.

And specifically in answer to your question, you need to use brackets within your regexp to extract into the $N variables.

Upvotes: 4

Related Questions