gpt4 book ai didi

bash - 尝试打印出包含最早/最晚日期的行

转载 作者:行者123 更新时间:2023-11-29 09:31:00 25 4
gpt4 key购买 nike

我不知道该怎么做。我的脚本将输出日期/时间本身,但不会输出包含该值的整行。

输入如下所示:

1 UID    PID PPID  C STIME TTY    TIME     CMD
2 adz110 5344 5334 0 08:47 pts /2 00:00:00 bash
3 dmq292 6908 6854 0 Jun04 pts /1 00:00:00 bash
4 adz110 7227 7150 0 Jul11 pts /9 00:00:00 who
5 erg474 7466 7461 0 08:54 pts /10 00:00:00 ls
6 dmq292 7966 7960 0 Jun04 pts /13 00:00:00 assign1 . sh if of
7 xle135 8983 8636 0 08:59 pts /15 00:00:00 ssh ctf . cs . utsarr . net
8 zeh458 9057 1980 0 08:59 pts /7 00:00:00 vim prog . c
9 rslavin 9150 9139 0 08:59 pts /16 00:00:00 ps - af
10 xle135 8636 8628 0 08:58 pts /15 00:00:00 bash

这似乎适用于打印出“最早开始时间:Jun04”和“最晚开始时间:08:59”。

min = $5; next} {if ($5 > min) min = $5;}
{max = 0}{if ($5>max) max=$5}

我正在完成整个脚本,如下所示:

sort |
gawk '
$1 !~ /[a-z]{3}[0-9]{3}/ {
next
}
NR==0{min = $5; next} {if ($5 > min) min = $5;}
{max = 0}{if ($5>max) max=$5}
prev!=$1 && prev{
print "User: " prev ORS "\t " val
prev=val=""
}
{
prev=$1
val=(val?val ORS "\t ":"")substr($0, index($0,$8))

} ;

END {
if(prev){
print "User: " prev ORS "\t " val
}; { print "\nEarliest Start Time :\n" } { print min };
{ print "\nLatest Start Time :\n" } { print max };
}'

我想我想将其更改为“min = $0”和“max = $0”以获得包含最小值和最大值的整行。但这会打印出不同的行。

样本输出应该是:

1 User : adz110
2 bash
3 who
4 User : dmq292
5 bash
6 assign1 . sh if of
7 User : erg474
8 ls
9 User : xle135
10 bash
11 ssh ctf . cs . utsarr . net
12 User : zeh458
13 vim prog . c
14
15 Earliest Start Time :
16 dmq292 6908 6854 0 Jun04 pts /1 00:00:00 bash
17
18 Latest Start Time :
19 xle135 8983 8636 0 08:59 pts /15 00:00:00 ssh ctf . cs . utsarr . net

对我来说,最早的开始时间是“Jun04”,最晚的开始时间是“08:59”

最佳答案

#! /bin/bash

file=$1

awk ' BEGIN {
mon["Jan"]=1
mon["Feb"]=2
mon["Mar"]=3
mon["Apr"]=4
mon["May"]=5
mon["Jun"]=6
mon["Jul"]=7
mon["Aug"]=8
mon["Sep"]=9
mon["Oct"]=10
mon["Nov"]=11
mon["Dec"]=12
}

# main logic here
NR != 1{

# storing user info
users[$1]=users[$1]"\n\t"$9
for (i = 10 ; i <= NF ; ++i)
users[$1]=users[$1]" "$i

# min
if (min != "") {
if (compare(min, $5) == 1) {
min=$5
min_line=$0
}
} else {
min=$5
min_line=$0
}

# max
if (max != "") {
if (compare(max, $5) == -1) {
max=$5
max_line=$0
}
} else {
max=$5
max_line=$0
}
}

# printing here
END {
for (user in users) {
print "User:", user, users[user]
}
print "Earliest Start Time :\n\t"min_line
print "Latest Start Time :\n\t"max_line
}

function compare(arg1, arg2) {
# returns
# -1 if arg1 < arg2
# 0 if arg1 == arg2
# 1 if arg1 > arg2

last_two_arg1=substr(arg1, 3)
last_two_arg2=substr(arg2, 3)

first_two_arg1=substr(arg1, 0, 2)
first_two_arg2=substr(arg2, 0, 2)

if (int(first_two_arg1) == first_two_arg1) {
# arg1 is a time
if (int(first_two_arg2) == first_two_arg2) {
# arg2 is a time aswell
if (first_two_arg1 > first_two_arg2) {
return 1
} else if (first_two_arg1 == first_two_arg2) {
if (last_two_arg1 > last_two_arg2)
return 1
else if (last_two_arg1 == last_two_arg2)
return 0
else
return -1
} else {
return -1
}
} else {
# arg2 is a date means is smaller
return 1
}
} else {
# arg1 is date
if (int(first_two_arg2) == first_two_arg2) {
# arg2 is time, means arg1 is smaller
return -1
} else {
# both arg1 and arg2 are date
first_three_arg1=substr(arg1, 0, 3)
first_three_arg2=substr(arg2, 0, 3)

if (mon[first_three_arg1] > mon[first_three_arg2])
return 1
else if (mon[first_three_arg1] == mon[first_three_arg2]) {
if (last_two_arg1 > last_two_arg2)
return 1
else if (last_two_arg1 == last_two_arg2)
return 0
else
return -1
} else {
return -1
}
}
}

}' "${file}"

编写此脚本的主要障碍是比较两种不同时间格式的函数。

上面的脚本应该会产生想要的结果,虽然我没有处理排序,如果需要的话,你可以问。

关于bash - 尝试打印出包含最早/最晚日期的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58262264/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com