CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Unwanted line feeds

 



charlesD
Novice

Apr 12, 2009, 8:39 AM

Post #1 of 3 (419 views)
Unwanted line feeds Can't Post

Hi,
This is my first post, and I am just cracking the book "O'Reilly CGI Programming" .
There is an example perl script I'm trying to customize, and I'm not having a lot of luck.
There's so much in the FAQ list, I couldn't find anything to answer my question.

Here's what I have so far:

#!/usr/bin/perl
print "Content-type: text/plain","\n\n";
$up = `/usr/bin/uptime | cut -d " " -f 5-8`;
$uptime = `/usr/bin/uptime`;
($load_average) = ($uptime =~ /averages: ([^,]*)/);
print "The server has been up for ", $up , "and the load average is : ", $load_average , ".", "\n";
exit (0);

And here's the output:

Content-type: text/plain

The server has been up for day, 2:06, 1
and the load average is : 0.00.

I would prefer it say:
"The server has been up for 1 day, 2:06, and the load average is: 0.00"
All on one line.

There's 2 things wrong.
1st the $up variable isn't appropriate because that's going to change and the cut -f parameter won't handle it right. I need a regexp that finds everything between "up" and the second comma.

2nd Why is there a line feed after the output of the $up variable?

edit: I'm using perl 5.8.9 on FreeBSD 6.4-STABLE

Hope all that's clear, and thanks in advance for any advice.
Charles


--
Thanks,
Charles

Using Perl since 4/12/09
perl 5.8.9, FreeBSD 6.4-STABLE
http://bubbabbq.homeunix.net

(This post was edited by charlesD on Apr 12, 2009, 8:46 AM)


KevinR
Veteran


Apr 12, 2009, 10:41 AM

Post #2 of 3 (415 views)
Re: [charlesD] Unwanted line feeds [In reply to] Can't Post

You can do this:


Code
#!/usr/bin/perl  
print "Content-type: text/plain","\n\n";
$up = `/usr/bin/uptime | cut -d " " -f 5-8`;
$uptime = `/usr/bin/uptime`;
print $uptime;


And that should show you why the line break is in there. The uptime function must be returning more than one line of output. If you wanted to remove line breaks you could do this before formatting the output:


Code
$uptime =~ s/[\r\n]//g;


As far as the cut-f problem goes, I don't know.
-------------------------------------------------


charlesD
Novice

Apr 12, 2009, 3:28 PM

Post #3 of 3 (412 views)
Re: [KevinR] Unwanted line feeds [In reply to] Can't Post

Well, thanks for what help you did provide.
It took me most of the afternoon to get things the way I wanted.
I'm going to close this thread and open a new one for help on the regexp.


--
Thanks,
Charles

Using Perl since 4/12/09
perl 5.8.9, FreeBSD 6.4-STABLE
http://bubbabbq.homeunix.net

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives