gpt4 book ai didi

linux - 在不同的输出线上进行操作

转载 作者:太空宇宙 更新时间:2023-11-04 12:27:33 25 4
gpt4 key购买 nike

我编写了一些脚本来安排文件的输出并打印第一个字段,时间如下: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 个值来获得某些过程的持续时间

最佳答案

我编写了以下 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

这两者的区别正是

May 27 02:43:40

May 27 02:56:55

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

关于linux - 在不同的输出线上进行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44210342/

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