By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,903 Members | 1,104 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,903 IT Pros & Developers. It's quick & easy.

Help in Perl script (newbiew)

P: 4
The soruce is the url to get the status of the server page
Output is as following between lines
------------------------------------------------------------------------------------------
Apache Server Status for test01

Server Version: IBM_HTTP_Server
Server Built: Apr 7 2003 13:38:46

Current Time: Tuesday, 19-Aug-2008 16:11:09 EDT
Restart Time: Sunday, 17-Aug-2008 07:44:21 EDT
Parent Server Generation: 0
Server uptime: 2 days 8 hours 26 minutes 48 seconds
Total accesses: 980721 - Total Traffic: 3.9 GB
CPU Usage: u25654.1 s5329.57 cu0 cs0 - 15.2% CPU load
4.83 requests/sec - 20.2 kB/second - 4285 B/request
6 requests currently being processed, 69 idle workers

------------------------------------------------------------------------------------------
I can get abve ouput in variable

I want to grab the underline and bold values in 4 variables and print those on screen

Thank you in advance.
Alex
Aug 19 '08 #1
Share this Question
Share on Google+
7 Replies


eWish
Expert 100+
P: 971
What have you tried? How much perl do you know. Please show use the code.

--Kevin
Aug 19 '08 #2

P: 4
I know the khs, I understand programming but very basic perl.

my code

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. #
  3. $output = `wget -q -O- --bind-address=localhost http://test:64999/server-status`; || die "Problems: $!"
  4. $output =~ /CPU Usage: u/;
  5. print "$1 ";
I tried for 1 value to print but no output on the screen.
If I get this working I'll try more in perl.

TIA

What have you tried? How much perl do you know. Please show use the code.

--Kevin
Aug 20 '08 #3

Ganon11
Expert 2.5K+
P: 3,652
I know the khs, I understand programming but very basic perl.

my code

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. #
  3. $output = `wget -q -O- --bind-address=localhost http://test:64999/server-status`; || die "Problems: $!"
  4. $output =~ /CPU Usage: u/;
  5. print "$1 ";
I tried for 1 value to print but no output on the screen.
If I get this working I'll try more in perl.

TIA
You're trying to print $1, but you haven't captured anything in the previous regexp. In fact, your regexp on line 4 doesn't do anything except match for "CPU Usage: u", and doesn't capture, change, or affect anything else.

Also, the "|| die" portion of your assignment in line 3 is after a semicolon, which should be giving you syntax errors (or would be if you had use strict and use warnings enabled).
Aug 20 '08 #4

eWish
Expert 100+
P: 971
Currently you are not capturing the data in your regex. If you want to capture then you will need to use a set of ().

Expand|Select|Wrap|Line Numbers
  1. $output =~ /(CPU Usage: u)/;
  2. print $1;
I would suggest that you check out perlre.

--Kevin
Aug 20 '08 #5

P: 4
thanks, I'll read the link you posted, meanwhile if you could pls show how to do for 1 var and then I'll try for others.

TIA
Aug 20 '08 #6

eWish
Expert 100+
P: 971
Here is an example to help you get started. In this example I am opening a file and reading it line by line and looking for a match according to the regex. If it finds a match then print it what was captured in $1.

Expand|Select|Wrap|Line Numbers
  1. open(my $FILE, '<', $test_file) || die "Can't open $test_file: $!\n";
  2. while(<$FILE>) {
  3.     chomp;
  4.     print $1 if /^(CPU Usage:.*?\w\d{4}.\d)/g;    
  5. }
  6. close($FILE);
Since regular expressions are not my strong point someone may point out a better solution.

--Kevin
Aug 20 '08 #7

P: 4
Thanks I'll try in the morning.

Here is an example to help you get started. In this example I am opening a file and reading it line by line and looking for a match according to the regex. If it finds a match then print it what was captured in $1.

Expand|Select|Wrap|Line Numbers
  1. open(my $FILE, '<', $test_file) || die "Can't open $test_file: $!\n";
  2. while(<$FILE>) {
  3.     chomp;
  4.     print $1 if /^(CPU Usage:.*?\w\d{4}.\d)/g;    
  5. }
  6. close($FILE);
Since regular expressions are not my strong point someone may point out a better solution.

--Kevin
Aug 20 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.