gpt4 book ai didi

AWK - 更改第二个日期

转载 作者:行者123 更新时间:2023-12-01 15:43:16 29 4
gpt4 key购买 nike

我有一个文件:

pablo tty8 Thu Nov 1 12:51:21 2012 still logged in 
(unknown tty8 Thu Nov 1 12:50:57 2012 - Thu Nov 1 12:51:21 2012 (00:00)
pablo tty2 Thu Nov 1 12:50:39 2012 still logged in
pablo tty7 Thu Nov 1 12:49:45 2012 - Thu Nov 1 12:50:56 2012 (00:01)
(unknown tty7 Thu Nov 1 12:34:32 2012 - Thu Nov 1 12:49:45 2012 (00:15)

我想把上面日期的文件替换一秒钟。我要打印:

pablo tty8 1351770681 still logged in 
(unknown tty8 1351770657 - 1351770681 (00:00)
pablo tty2 1351770639 still logged in
pablo tty7 1351770585 - 1351770656 (00:01)
(unknown tty7 1351769672 - 1351770585 (00:15)

我试过这个命令:

gawk --posix 'function my()
{"date -d \047"$0"\047 +%s" | getline b;
gsub( /[A-Za-z]{3} [A-Za-z]{3} [0-9] ([0-9]{2}:){2}[0-9]{2} [0-9]{4}/,b ); print}
{ my() }' file

上面的命令不起作用:

$ gawk --posix 'function my()
> {"date -d \047"$0"\047 +%s" | getline b;
> gsub( /[A-Za-z]{3} [A-Za-z]{3} [0-9] ([0-9]{2}:){2}[0-9]{2} [0-9]{4}/,b ); print}
> { my() }' ta
date: błędna data: `pablo tty8 Thu Nov 1 12:51:21 2012 still logged in '
pablo tty8 still logged in
(unknown tty8 1351897200 - 1351897200 (00:00)
date: błędna data: `pablo tty2 Thu Nov 1 12:50:39 2012 still logged in '
pablo tty2 1351897200 still logged in
date: błędna data: `pablo tty7 Thu Nov 1 12:49:45 2012 - Thu Nov 1 12:50:56 2012 (00:01) '
pablo tty7 1351897200 - 1351897200 (00:01)
(unknown tty7 1351897200 - 1351897200 (00:15)

如何改进上面的命令?

感谢您的帮助。

最佳答案

如果你安装了 vim,试试这个命令:

:%s/\v\w+\s\w+\s\d+\s\d+:\d+:\d+\s\d+/\=system('date +%s -d"'.submatch(0).'" | tr -d "\n"')/g

这个想法很简单。 vim 可以非常快。

关于AWK - 更改第二个日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13208379/

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