For Programmers: Free Programming Magazines  


Home > Archive > AWK > November 2007 > Return just the text I want from "uptime"









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Return just the text I want from "uptime"
Eliot

2007-11-05, 7:57 am

I want to use the system command "uptime" and then use awk to display
just the text I am interested in:-
Consider the following three lines of input
18:01:04 up 1 min, 0 users, load average: 0.55, 0.28, 0.10
22:14:47 up 1:01, 0 users, load average: 0.21, 0.05, 0.02
17:58:52 up 1 day, 2:34, 0 users, load average: 0.02, 0.02, 0.00
The output for each line should be
1 min
1:01
1 day, 2:34
Of course I only need to send one line at a time to awk, I am
struggling because the number of fields varies.

Janis Papanagnou

2007-11-05, 7:57 am

Eliot wrote:
> I want to use the system command "uptime" and then use awk to display
> just the text I am interested in:-
> Consider the following three lines of input
> 18:01:04 up 1 min, 0 users, load average: 0.55, 0.28, 0.10
> 22:14:47 up 1:01, 0 users, load average: 0.21, 0.05, 0.02
> 17:58:52 up 1 day, 2:34, 0 users, load average: 0.02, 0.02, 0.00
> The output for each line should be
> 1 min
> 1:01
> 1 day, 2:34
> Of course I only need to send one line at a time to awk, I am
> struggling because the number of fields varies.
>


One possibility...

awk '{sub(/^.* up +/,"");sub(/, *[0-9]+ users.*/,"");print}'


Janis
Eliot

2007-11-05, 7:57 am

On 5 Nov, 13:41, Janis Papanagnou <Janis_Papanag...@hotmail.com>
wrote:
> Eliot wrote:
>
> One possibility...
>
> awk '{sub(/^.* up +/,"");sub(/, *[0-9]+ users.*/,"");print}'
>
> Janis


Thanks you very much, now I am going to try and figure out that works!

Kenny McCormack

2007-11-05, 6:58 pm

In article <1194269755.937838.186160@50g2000hsm.googlegroups.com>,
Eliot <ebp@hotmail.com> wrote:
>I want to use the system command "uptime" and then use awk to display
>just the text I am interested in:-
>Consider the following three lines of input
> 18:01:04 up 1 min, 0 users, load average: 0.55, 0.28, 0.10
> 22:14:47 up 1:01, 0 users, load average: 0.21, 0.05, 0.02
> 17:58:52 up 1 day, 2:34, 0 users, load average: 0.02, 0.02, 0.00
>The output for each line should be
>1 min
>1:01
>1 day, 2:34
>Of course I only need to send one line at a time to awk, I am
>struggling because the number of fields varies.


Assuming you are running under Linux (a leap, yes, but probably a pretty
good bet), let me say that I tried this once and although seemingly
simple, it gets ugly after a while. Instead, I ended up parsing
/proc/uptime directly (which is actually not that hard, once you get
over the initial idea).

This is assuming that you actually want to *do* something with the
uptime (I.e., parse it into some kind of actual time representation),
not just display it.

Patrick TJ McPhee

2007-11-05, 9:58 pm

In article <1194269755.937838.186160@50g2000hsm.googlegroups.com>,
Eliot <ebp@hotmail.com> wrote:
% I want to use the system command "uptime" and then use awk to display
% just the text I am interested in:-
% Consider the following three lines of input
% 18:01:04 up 1 min, 0 users, load average: 0.55, 0.28, 0.10
% 22:14:47 up 1:01, 0 users, load average: 0.21, 0.05, 0.02
% 17:58:52 up 1 day, 2:34, 0 users, load average: 0.02, 0.02, 0.00
% The output for each line should be
% 1 min
% 1:01
% 1 day, 2:34

BEGIN { FS="up |," }
{ print $2 }


--

Patrick TJ McPhee
North York Canada
ptjm@interlog.com
Janis Papanagnou

2007-11-05, 9:58 pm

Patrick TJ McPhee wrote:
> In article <1194269755.937838.186160@50g2000hsm.googlegroups.com>,
> Eliot <ebp@hotmail.com> wrote:
> % I want to use the system command "uptime" and then use awk to display
> % just the text I am interested in:-
> % Consider the following three lines of input
> % 18:01:04 up 1 min, 0 users, load average: 0.55, 0.28, 0.10
> % 22:14:47 up 1:01, 0 users, load average: 0.21, 0.05, 0.02
> % 17:58:52 up 1 day, 2:34, 0 users, load average: 0.02, 0.02, 0.00
> % The output for each line should be
> % 1 min
> % 1:01
> % 1 day, 2:34
>
> BEGIN { FS="up |," }
> { print $2 }
>
>


That doesn't cover the OPs requirements (see output line 3).
The FS must be refined, e.g.

BEGIN { FS="up +|, *[0-9]+ users" }
{ print $2 }


Janis
Claudio

2007-11-09, 9:57 pm

On 2007-11-05, Eliot <ebp@hotmail.com> wrote:
> I want to use the system command "uptime" and then use awk to display
> just the text I am interested in:-
> Consider the following three lines of input
> 18:01:04 up 1 min, 0 users, load average: 0.55, 0.28, 0.10
> 22:14:47 up 1:01, 0 users, load average: 0.21, 0.05, 0.02
> 17:58:52 up 1 day, 2:34, 0 users, load average: 0.02, 0.02, 0.00
> The output for each line should be
> 1 min
> 1:01
> 1 day, 2:34
> Of course I only need to send one line at a time to awk, I am
> struggling because the number of fields varies.
>


uptime | awk -F"up" '{print $2}' | awk -F",[^,]*users" '{print $1}'

Consider the use of "sed" instead.

Best regards,
Claudio.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com