【问题标题】:Make operations on different output lines在不同的输出线上进行操作
【发布时间】:2017-05-26 21:27:41
【问题描述】:

我编写了一些脚本来安排文件的输出并打印第一个字段,时间是这样的:15:23:30

我尝试通过将第一个字段的每个部分相乘以将其转换为秒来转换第一个字段,然后在每两行之间进行减法,但我不能将每一行都作为参数来在每两行之间进行运算.

for rec in $(cut -d " " -f2 file.txt | uniq -d); do
   grep -- "$rec" file.txt done |
awk '{split($0,a," "); print a[1] }'

输入文件类似于 /var/log/messages

May 27 02:43:40 Rolly NetworkManager[2411]: <info> (eth0): DHCPv4 state changed renew -> renew
May 27 02:43:40 Rolly NetworkManager[2411]: <info>   address 192.168.159.133
May 27 02:43:40 Rolly NetworkManager[2411]: <info>   prefix 24 (255.255.255.0)
May 27 02:43:40 Rolly NetworkManager[2411]: <info>   gateway 192.168.159.2
May 27 02:43:40 Rolly NetworkManager[2411]: <info>   nameserver '192.168.159.2'
May 27 02:43:40 Rolly NetworkManager[2411]: <info>   domain name 'localdomain'
May 27 02:56:55 Rolly dhclient[2481]: DHCPREQUEST on eth0 to 192.168.159.254 port 67 (xid=0x3bb3abec)
May 27 02:56:55 Rolly dhclient[2481]: DHCPACK from 192.168.159.254 (xid=0x3bb3abec)
May 27 02:56:55 Rolly dhclient[2481]: bound to 192.168.159.133 -- renewal in 795 seconds.
May 27 02:56:55 Rolly NetworkManager[2411]: <info> (eth0): DHCPv4 state changed renew -> renew

所需的输出应该是时间列 然后对它的某些行进行一些操作

我想得到类似的输出

1m 25s

如果我可以通过减去转换时间的 2 个值来获得某些过程的持续时间

【问题讨论】:

  • 您可以添加输入示例和所需输出吗?那真的很有帮助。
  • “对它的某些行进行一些操作”? 我想你应该写一个脚本。如果您不告诉我们您要执行哪些操作,我们将无法告诉您如何操作。
  • 我在上面提到过:通过将第一个字段乘以它的每个部分将其转换为秒,然后在每两行之间进行减法
  • edit 你的问题显示给定输入的预期输出。见How to Ask
  • 我想将时间列转换为秒来计算它们的一些记录的减法,但我不能

标签: linux shell awk sed


【解决方案1】:

我写了以下awk 脚本:

# initiate mapping array "Jan"->"01", "Feb"->"02", etc.
BEGIN {
    # assume all dates are from the current year
    YYYY = strftime("%Y", systime())

    # use first day of the month to extract month abbreviations
    # Note: Any day from "01" to "28" would work here.
    DD = "01"

    # use midnight as time for month abbreviation extraction
    # Note: Any (valid) time would work here.
    HH = "00"
    MM = HH
    SS = MM

    # assemble time string ("MM HH SS" format)
    timestr = MM " " HH " " SS

    # iterate over all months
    for (month = 1; month <= 12; ++month) {

        # convert month number to two-digit string ("MM" format)
        MM = sprintf("%02i", month)

        # assemble date string ("YYYY MM DD" format)
        datestr = YYYY " " MM " " DD

        # create timestamp (needs "YYYY MM DD HH MM SS" format)
        datetime = mktime(datestr " " timestr)

        # extract month abbreviation ("Jan", "Feb", etc.)
        monthstr = strftime("%b", datetime)

        # map month abbreviation to month string ("MM" format)
        monthstr2MM[monthstr] = MM
    }
}

# read timestamp from log input
{
    # convert month abbreviation in 1st field to string ("MM" format)
    MM = monthstr2MM[$1]

    # extract date from 2nd field (already in "DD" format)
    DD = $2

    # assemble date string ("YYYY MM DD" format)
    # Note: YYYY still is the current year for all timestamps. So this
    # will fail if the log spans the change from one year to the next.
    datestr = YYYY " " MM " " DD

    # extract time string from 2nd field ("HH:MM:SS" format)
    timestr=$3

    # convert time string to "HH MM SS" format
    gsub(":"," ",timestr)

    # create timestamp from "YYYY MM DD HH MM SS" format
    datetime = mktime(datestr " " timestr)
}

# if current timestamp differs from previous: print time difference
(NR != 1) && (datetime != previous) {

    # calculate time difference (in seconds)
    timediff = datetime - previous

    # print minutes and seconds passed since previous timestamp
    printf "%im %is\n",int(timediff/60),timediff%60
}

# store current timestamp for comparison with the next one
{
    previous = datetime
}

如果现在我将其保存为 timediff.awk 并将您的示例输入保存为 log.file 并运行

awk -f timediff.awk < log.file

我明白了

13m 15s

这正是两者的区别

5 月 27 日 02:43:40

5 月 27 日 02:56:55

如果您需要进一步的解释/调整,请告诉我。

【讨论】:

  • 非常感谢这是完美脚本,对我来说有些功能是新的,比如mktime,但previous部分我没有得到它
  • awk 为输入的每个 recordi.e. 行)运行此代码。因此,要将当前行与前一行进行比较,我们必须将当前时间戳保存在一个变量中(在处理下一条记录时该变量仍然可用)。所以我将datetime 复制到previous,因为datetime 将在处理下一个*record*/line 时被覆盖,但previous 仍将具有“旧”值,因此我可以比较两者。我可以将它移到前一个块中,以便仅在需要时更新previous,但是我们需要另一个块第一次设置它(NR==1)。我希望能澄清一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-09
  • 1970-01-01
  • 2016-04-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多