我在bash中使用正常运行时间以获取机器的当前运行时间。我需要抓住时间并显示2天,12小时,23分钟的格式。
答案 0 :(得分:20)
我的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,
第九个字段,最后是字符串{{1} }。
从重新启动开始,我的minutes.
生成如下输出:
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
启用扩展正则表达式语法。 (在较旧的GNU seds上,使用-E
代替-r
)
-E
此替换命令会删除最多s/^[^,]*up *//
。
up
此替换命令删除用户计数及其后面的所有文本。
s/, *[[:digit:]]* users.*//
这会将s/min/minutes/
替换为min
。
minutes
如果该行包含hh:mm格式的时间,则会将小时与分钟分开,并将其替换为s/([[:digit:]]+):0?([[:digit:]]+)/\1 hours, \2 minutes/'
。
hh hours, mm minutes
在与上述相同的测试用例中,这会产生:
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."}'
对于那些喜欢分散在多行的awk代码的人:
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.
答案 1 :(得分:13)
仅仅是完整性......关于:
$ uptime -p
up 2 weeks, 3 days, 14 hours, 27 minutes
答案 2 :(得分: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.
答案 3 :(得分:1)
为此:
更简单的是:
uptime -p | cut -d " " -f2-
答案 4 :(得分:1)
我为支持uptime -p
的系统(例如较新的linux)和不支持#!/bin/sh
uptime -p >/dev/null 2>&1
if [ "$?" -eq 0 ]; then
# Supports most Linux distro
# when the machine is up for less than '0' minutes then
# 'uptime -p' returns ONLY 'up', so we need to set a default value
UP_SET_OR_EMPTY=$(uptime -p | awk -F 'up ' '{print $2}')
UP=${UP_SET_OR_EMPTY:-'less than a minute'}
else
# Supports Mac OS X, Debian 7, etc
UP=$(uptime | sed -E 's/^[^,]*up *//; s/mins/minutes/; s/hrs?/hours/;
s/([[:digit:]]+):0?([[:digit:]]+)/\1 hours, \2 minutes/;
s/^1 hours/1 hour/; s/ 1 hours/ 1 hour/;
s/min,/minutes,/; s/ 0 minutes,/ less than a minute,/; s/ 1 minutes/ 1 minute/;
s/ / /; s/, *[[:digit:]]* users?.*//')
fi
echo "up $UP"
的系统(例如Mac OS X)制作了通用的shell脚本。
filename = 'words.txt'
try:
with open('G:\\Nalpha.txt', 'r') as fileobject:
contents = fileobject.read()
except FileNotFoundError:
message = 'Sorry, the file ' + filename + ' connot be found.'
print(message)
else:
words = contents.split()
number_words = len(words)
print('The file ' + filename + ' has approximatley ' + str(number_words) + ' words.')
alphabet = ('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z')
unique_words = []
'''Word Frequency'''
words = contents.split()
for word in words:
if word not in unique_words:
unique_words.append(word)
else:
pass
for i in unique_words:
word_frequencey = words.count(i)
print(i, ':', word_frequencey)
textfile = open('G:\\Nalphadone.txt', 'w')
textfile.write(word)
textfile.close()
通过我自己的自定义引用John1024答案。
答案 5 :(得分:1)
解决方案:为了在几秒钟内获得linux正常运行时间,请转至bash并键入cat /proc/uptime
。解析第一个数字并根据您的要求进行转换。
来自RedHat documentation:
此文件包含详细信息,说明系统自上次重新启动以来已开机多长时间。
/proc/uptime
的输出非常小:350735.47 234388.90
第一数字是系统经过的总秒数 上。
第二数字是机器花费了多少时间 闲置中 秒。
答案 6 :(得分: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 ]
答案 7 :(得分:0)
uptime_minutes() {
set `uptime -p`
local minutes=0
shift
while [ -n "$1" ]; do
case $2 in
day*)
((minutes+=$1*1440))
;;
hour*)
((minutes+=$1*60))
;;
minute*)
((minutes+=$1))
;;
esac
shift
shift
done
echo $minutes
}