我不知道该怎么办。我的脚本将输出日期/时间本身,但不输出包含该值的整行。
输入如下:

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

这似乎适用于打印“最早开始时间:6月4日”和“最晚开始时间: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”,以得到包含min和max值的整行。但那会打印出不同的行。
样本输出应为:
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

对我来说,最早的开始时间是6月4日,最晚的开始时间是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}"

编写这个脚本的主要障碍是一个比较两种不同时间格式的函数。
上面的脚本应该会产生预期的结果,虽然我还没有考虑排序,如果需要的话,可以问一下。

10-08 02:02