Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How to grep the string in perl.

Variable sessionWeb contains a web page html code. I want to grep "active Sessions" from this and want to print as below. For example the line in the web page will be

"2 active Sessions, 0 passivated Sessions (more stats...)"

Expected Output:

2 active Sessions.

I have tried the following code but it is not working ?

 $sessionStr="active Sessions";
     $sessionCount =~ grep( /$sessionStr/,$sessionWeb);
share|improve this question

closed as off topic by voretaq7 Apr 27 '12 at 6:53

Questions on Server Fault are expected to relate to server, networking, or related infrastructure administration within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

While we have traditionally welcomed questions about sysadmin-level scripting, this sort of question is really a better fit for Stack Overflow or Unix & Linux – voretaq7 Apr 27 '12 at 6:54
up vote 1 down vote accepted

This should to the trick:

if ($sessionWeb =~ /(\d+ active Sessions)/) {
  print $1."\n";
share|improve this answer
It is printing the entire html page – Arun Apr 27 '12 at 6:45
The above code extracts only the requested part. There must be a bug in your code. – Oliver Apr 27 '12 at 6:49

Not the answer you're looking for? Browse other questions tagged or ask your own question.