仁慈的 sed 霸主,这是我的原始输出:
reboot system boot 3.10.0-327.28.3. Sun Jan 10 02:16 - 06:52 (04:36)
reboot system boot 3.10.0-327.28.3. Wed Jan 6 02:00 - 06:52 (4+04:52)
reboot system boot 3.10.0-327.28.3. Sun Jan 3 00:39 - 06:52 (7+06:13)
reboot system boot 3.10.0-327.28.3. Sat Jan 2 02:40 - 06:52 (8+04:12)
reboot system boot 3.10.0-327.28.3. Sat Jan 2 02:39 - 06:52 (8+04:13)
reboot system boot 3.10.0-327.28.3. Wed Dec 30 06:56 - 06:52 (10+23:56)
reboot system boot 3.10.0-327.28.3. Wed Dec 30 02:43 - 03:17 (00:34)
reboot system boot 3.10.0-327.28.3. Wed Dec 30 00:51 - 03:17 (02:26)
reboot system boot 3.10.0-327.28.3. Tue Dec 29 07:14 - 03:17 (20:02)
reboot system boot 3.10.0-327.28.3. Tue Dec 29 05:30 - 07:14 (01:44)
reboot system boot 3.10.0-327.28.3. Tue Dec 29 05:18 - 07:14 (01:55)
我的愿望是用 sed 单行删除最后一列中的括号,而不是使用这个几乎非常明显的不优雅且菜鸟般的片段:
[root@OEL7 ~]# last reboot | sed -e 's/(//g' | sed -e 's/)//g'
reboot system boot 3.10.0-327.28.3. Sun Jan 10 02:16 - 07:12 04:55
reboot system boot 3.10.0-327.28.3. Wed Jan 6 02:00 - 07:12 4+05:11
reboot system boot 3.10.0-327.28.3. Sun Jan 3 00:39 - 07:12 7+06:32
reboot system boot 3.10.0-327.28.3. Sat Jan 2 02:40 - 07:12 8+04:31
reboot system boot 3.10.0-327.28.3. Sat Jan 2 02:39 - 07:12 8+04:32
reboot system boot 3.10.0-327.28.3. Wed Dec 30 06:56 - 07:12 **11+00:15**
reboot system boot 3.10.0-327.28.3. Wed Dec 30 02:43 - 03:17 00:34
reboot system boot 3.10.0-327.28.3. Wed Dec 30 00:51 - 03:17 02:26
reboot system boot 3.10.0-327.28.3. Tue Dec 29 07:14 - 03:17 20:02
reboot system boot 3.10.0-327.28.3. Tue Dec 29 05:30 - 07:14 01:44
reboot system boot 3.10.0-327.28.3. Tue Dec 29 05:18 - 07:14 01:55
我相信我在发布之前已经进行了尽职调查(包括在发布此帖子之前筛选了一些建议的帖子),但我发现的示例不符合我的标准:
- 强制使用 sed
- 以尽可能短的方式实现结果,无需管道等。
如果你们中的任何人知道我可能错过的解决方案,我将永远感激任何 URL、指针等。
最佳答案
IIUC,您只需要:
last reboot | sed 's,[()],,g'
关于regex - 在 sed 单行代码中用空格替换/删除 2 个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65654968/