Help - Search - Members - Calendar
Full Version: Uptime | Cut
Linuxhelp > Support > Technical Support
uptime | cut -c -38

Shows (usually) most of uptime, with "load average 0.0 0.0 0.0" cut out. But since, depending on how many users are logged on, and what time/date it is, sometimes using this command can cut off past of the word "users". sad.gif

I'm not very good at stream-editing, or whatever it's called, so I was wondering if anyone knew of a good tutorial, or could help me out with this problem? biggrin.gif Thanks!
There are several different ways to do this, some more complicated than others.

Using Awk:

| awk '{
  # chops off "up" and everything before it:
  sub(/.*up[ ]+/,"",$0)
  # chops off ", # users" and everything after it:     
  sub(/,[ ]+[0-9]+ user.*/,"",$0)
  # cleans up extra spaces, i think:   
  # obvious enough, prints the results                     

uptime | sed -e 's/.* (.* days,)? (.*:..,) .*/1 2/' -e's/,//g' -e 's/ days/d/' -e 's/ up //'

#  upt - show just the system uptime, days, hours, and minutes

let upSeconds="$(cat /proc/uptime) && echo ${temp%%.*})"
let secs=$((${upSeconds}%60))
let mins=$((${upSeconds}/60%60))
let hours=$((${upSeconds}/3600%24))
let days=$((${upSeconds}/86400))
if [ "${days}" -ne "0" ]
  echo -n "${days}d"
echo -n "${hours}h${mins}m"

# The line above is needed to tell the script
# where to look for the perl program
# After creating a script, you need to tell the computer it's
# an executable program. Do this by typing:
# chmod 755 /path_to_script/name_of_your_script

# 'uptime' is the name of a small command-line application that prints
# the time the server has been running, and the load on the cpu
# What I do below is take the output of 'uptime' and
# store it in a string variable

$result_of_uptime_command = `uptime`;

# This is the result of a typical <uptime> command.
# We are interested in the number and time unit between the words
# right after "up". (1 day in this example below)
# 11:38am up 1 day, 23:30, 1 user, load average: 1.39, 1.57, 1.79
# ^^^^^^
# To extract the number from the string use "split" to break it into parts
# $part1 equals everything before "up". $part2 is everything after "up"

($part1, $part2) = split(/up /,$result_of_uptime_command);

# Given the example <uptime> output from above,
# this is what we expect the parts to be:
# $part1 = " 11:38am up "
# $part2 = "1 day, 23:30, 1 user, load average: 1.39, 1.57, 1.79"

# Now take $part2 and split it into a number and the time units

($time_integer, $time_unit) = split(/ /,$part2);

# Given the example <uptime> output from above,
# this is what we expect the parts to be:
# $time_integer = "1"
# $time_unit = "day, 23:30, 1 user, load average: 1.39, 1.57, 1.79"
# We don't care that $time_unit has all that extra crap after the word day.
# We can easily filter it out. We only care about the first few letters
# which will tell us mins or days. Unfortunately, it won't tell us
# the number of hours. We will have to make a special case for
# 1 to 23 hours. Bummer.

# Are you calling this script from a browser? Then uncomment the next line.
#print "Content-type: text/htmlnn";

# The next chuck is an "if (then) else" loop.
# The first line asks: If the time is not empty (!= ""), then do the loop.

if ($time_integer != "")

# If time is one, that is 1 min or 1 day, we need don't want "s"
# The symbol =~ checks for a substring match.

if ($time_integer == "1"){

if ($time_unit =~ /min/){$min_hr_day = "minute";
if ($time_unit =~ /day/){$min_hr_day = "day";

# Time must not be one, so we need the plural units

else {
if ($time_unit =~ /min/){$min_hr_day = "minutes";
if ($time_unit =~ /day/){$min_hr_day = "days";

# Cool. We now have an integer and a time unit.
# Let's print them! We're done.

print "$time_integer $min_hr_dayn";


# If we get to this "else", that means we are in that awkward first 24 hour
# period and must carve the time into hours and minutes.
# For example, <uptime> lists those first hours as "2:44," so we can use split
# is based on the colon. The "chop" command gets rid of the trailing comma.

else {

($time_hours, $time_minutes) = split(/:/,$time_unit);

# Cool. We have the number of hours and minutes.
# Let's print them. We're done.

print "n$time_hours hrs, ";
chop $time_minutes;
print "$time_minutes minn";

Note: i haven't tested any of these, so check them out smile.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2018 Invision Power Services, Inc.