是否可以将日期格式从示例更改Fri 12 Feb 18:27:34 +08 2021
为简单的DD-MM-YYYY
?
具体来说,要更改last
命令中的日期格式
答案1
你不能直接这样做。阅读文档 ( man last
) 建议使用该--time-format
选项,但仅提供一些选择,并且没有用户可定义的格式。这是默认的输出格式:
last
roaima pts/0 10.1.1.16 Sat Feb 13 16:21 still logged in
roaima pts/1 :pts/0:S.0 Mon Feb 8 13:42 - 22:47 (09:05)
roaima pts/0 10.1.1.16 Mon Feb 8 13:42 - 22:47 (09:05)
roaima pts/2 10.1.1.16 Fri Jan 15 13:57 - 02:04 (12:06)
reboot system boot 4.19.0-13-amd64 Tue Jan 12 01:19 still running
wtmp begins Fri Jan 8 10:10:13 2021
幸运的是,其中一个选项 ( iso
) 以相对容易编辑的格式显示日期:
last --time-format iso
roaima pts/0 10.1.1.16 2021-02-13T16:21:47+00:00 still logged in
roaima pts/1 :pts/0:S.0 2021-02-08T13:42:08+00:00 - 2021-02-08T22:47:14+00:00 (09:05)
roaima pts/0 10.1.1.16 2021-02-08T13:42:07+00:00 - 2021-02-08T22:47:14+00:00 (09:05)
roaima pts/3 10.1.1.16 2021-01-15T13:57:27+00:00 - 2021-01-16T02:04:26+00:00 (12:06)
reboot system boot 4.19.0-13-amd64 2021-01-12T01:19:18+00:00 still running
wtmp begins 2021-01-08T10:10:13+00:00
从 ISO 格式的日期戳中剥离时间部分可以得到:
last --time-format iso | sed -E 's/T[[:digit:]:+]{14}//g'
roaima pts/0 10.1.1.16 2021-02-13 still logged in
roaima pts/1 :pts/0:S.0 2021-02-08 - 2021-02-08 (09:05)
roaima pts/0 10.1.1.16 2021-02-08 - 2021-02-08 (09:05)
roaima pts/3 10.1.1.16 2021-01-15 - 2021-01-16 (12:06)
reboot system boot 4.19.0-13-amd64 2021-01-12 still running
wtmp begins 2021-01-08
如果您确实想要其中的日期格式,那么dd-mm-yyyy
它会变得更加复杂,因为我们必须解析出日、月、年并按照您的顺序重新组合它们
last --time-format iso | sed -E 's/([[:digit:]]{4})-([[:digit:]]{2})-([[:digit:]]{2})T[[:digit:]:+]{14}/\3-\2-\1/g'
roaima pts/0 10.1.1.16 13-02-2021 still logged in
roaima pts/1 :pts/0:S.0 08-02-2021 - 08-02-2021 (09:05)
roaima pts/0 10.1.1.16 08-02-2021 - 08-02-2021 (09:05)
roaima pts/3 10.1.1.16 15-01-2021 - 16-01-2021 (12:06)
reboot system boot 4.19.0-13-amd64 12-01-2021 still running
wtmp begins 08-01-2021
(如果您sed
没有该-E
标志或其等效标志-r
,请将其删除并在这四个字符(
, )
, {
,的每个实例前面}
加上反斜杠。RE 只会变得更难以阅读。)