tags:

views:

75

answers:

2
#!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.

+4  A: 
#!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.

ar
'In the United States, "bracket" usually refers specifically to the "square" or "box" type; in British usage, it normally refers to a parenthesis mark.' - http://en.wikipedia.org/wiki/Bracket. The unqualified term "bracket" is hence best avoided.
ysth
+2  A: 

$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.

ysth
(m/(\w*Svr)\b/)....works well($line =~ m/Svr\b/) ----chose by me does not work..Whats the bigg diff..Why mine is wrong
Sreeja
@Sreeja: yours doesn't have the () that show what to put in $1
ysth
Ok.Got it...thanks
Sreeja