2015-02-05 72 views
4

我在bash中使用正常運行時間以獲取計算機的當前運行時間。我需要抓緊時間,並顯示2天,12小時,23分鐘的格式。Bash格式正常運行時間以顯示日期,小時,分鐘

+0

http://unix.stackexchange.com/questions/34017/convert-linux-sysuptime-to-well-format-date – 2015-02-05 20:31:51

+2

隨着最近procps,你可以使用'uptime -p'。這出現在3.3.4和3.3.9之間。 – Wintermute 2015-02-05 20:41:25

回答

9

uptime產生輸出看起來像:

$ uptime 
12:49:10 up 25 days, 21:30, 28 users, load average: 0.50, 0.66, 0.52 

將其轉換成您的格式:

$ uptime | awk -F'(|,|:)+' '{print $6,$7",",$8,"hours,",$9,"minutes."}' 
25 days, 21 hours, 34 minutes. 

它是如何工作

  • -F'(|,|:)+'

    awk將其輸入分爲多個字段。這告訴awk將一個或多個空格,逗號或冒號的任意組合用作字段分隔符。

  • print $6,$7",",$8,"hours,",$9,"minutes."

    這告訴awk的打印第六字段和第七字段(由空格分隔),接着是逗號,第8場,字符串hours,第九字段,以及最後,字符串minutes.

短的運行時間處理電腦使用的sed

從重啓開始,我uptime產生輸出,如:

03:14:20 up 1 min, 2 users, load average: 2.28, 1.29, 0.50 
04:12:29 up 59 min, 5 users, load average: 0.06, 0.08, 0.48 
05:14:09 up 2:01, 5 users, load average: 0.13, 0.10, 0.45 
03:13:19 up 1 day, 0 min, 8 users, load average: 0.01, 0.04, 0.05 
04:13:19 up 1 day, 1:00, 8 users, load average: 0.02, 0.05, 0.21 
12:49:10 up 25 days, 21:30, 28 users, load average: 0.50, 0.66, 0.52 

以下sed命令處理這些格式:

uptime | sed -E 's/^[^,]*up *//; s/, *[[:digit:]]* users.*//; s/min/minutes/; s/([[:digit:]]+):0?([[:digit:]]+)/\1 hours, \2 minutes/' 

隨着上述時間,這產生:

1 minutes 
59 minutes 
2 hours, 1 minutes 
1 day, 0 minutes 
1 day, 1 hours, 0 minutes 
25 days, 21 hours, 30 minutes 

它是如何工作

  • -E打開擴展正則表達式語法。 (在舊版本的GNU SEDS,代替-E使用-r

  • s/^[^,]*up *//

    這種替換命令刪除所有文字達up

  • s/, *[[:digit:]]* users.*//

    這個替代命令刪除用戶數和跟隨其後的所有文本。

  • s/min/minutes/

    這將替換minminutes

  • s/([[:digit:]]+):0?([[:digit:]]+)/\1 hours, \2 minutes/'

    如果行包含以hh時間:mm格式,這從分鐘分離小時,並用hh hours, mm minutes替換它。

使用AWK

uptime | awk -F'(|,|:)+' '{if ($7=="min") m=$6; else {if ($7~/^day/) {d=$6;h=$8;m=$9} else {h=$6;m=$7}}} {print d+0,"days,",h+0,"hours,",m+0,"minutes."}' 

短的運行時間在相同的測試案例如上處理計算機,這將產生:

0 days, 0 hours, 1 minutes. 
0 days, 0 hours, 59 minutes. 
0 days, 2 hours, 1 minutes. 
1 days, 0 hours, 0 minutes. 
1 days, 1 hours, 0 minutes. 
25 days, 21 hours, 30 minutes. 

對於那些誰喜歡awk的代碼分佈在多行:

uptime | awk -F'(|,|:)+' '{ 
    if ($7=="min") 
     m=$6; 
    else { 
     if ($7~/^day/) { d=$6; h=$8; m=$9} 
     else {h=$6;m=$7} 
     } 
    } 
    { 
     print d+0,"days,",h+0,"hours,",m+0,"minutes." 
    }' 
1

對於各種的緣故,這裏的用sed的一個例子:

我的原始輸出:

$ uptime 
15:44:56 up 3 days, 22:58, 7 users, load average: 0.48, 0.40, 0.31 

轉換輸出:

$uptime|sed 's/.*\([0-9]\+ days\), \([0-9]\+\):\([0-9]\+\).*/\1, \2 hours, \3 minutes./' 
3 days, 22 hours, 58 minutes. 
9

只是VOR完整性......什麼是關於:

$ uptime -p 
up 2 weeks, 3 days, 14 hours, 27 minutes 
+2

應該是一個被接受的答案。在Ubuntu上適用於我,但不適用於OSX。 – SerCe 2016-12-12 00:09:33

0

這個答案對於OS X中發佈的uptime非常具體,但考慮到了輸出情況。

#!/bin/bash 

INFO=`uptime` 
echo $INFO | awk -F'[ ,:\t\n]+' '{ 
     msg = "↑ " 
     if ($5 == "day" || $5 == "days") {  # up for a day or more 
      msg = msg $4 " " $5 ", " 

      n = $6 
      o = $7 
     } else { 
      n = $4 
      o = $5 
     } 

     if (int(o) == 0) {      # words evaluate to zero 
      msg = msg int(n)" "o 
     } else {        # hh:mm format 
      msg = msg int(n)" hr" 
      if (n > 1) { msg = msg "s" } 

      msg = msg ", " int(o) " min" 
      if (o > 1) { msg = msg "s" } 
     } 

     print "[", msg, "]" 
    }' 

一些示例可能的輸出:

22:49 up 24 secs, 2 users, load averages: 8.37 2.09 0.76 
[ ↑ 24 secs ] 

22:50 up 1 min, 2 users, load averages: 5.59 2.39 0.95 
[ ↑ 1 min ] 

23:39 up 51 mins, 3 users, load averages: 2.18 1.94 1.74 
[ ↑ 51 mins ] 

23:54 up 1:06, 3 users, load averages: 3.67 2.57 2.07 
[ ↑ 1 hr, 6 mins ] 

16:20 up 120 days, 10:46, 3 users, load averages: 1.21 2.88 0.80 
[ ↑ 120 days, 10 hrs, 46 mins ] 
相關問題