regex - 在 sed 单行代码中用空格替换/删除 2 个字符

标签 regex sed

仁慈的 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

我相信我在发布之前已经进行了尽职调查(包括在发布此帖子之前筛选了一些建议的帖子),但我发现的示例不符合我的标准:

  1. 强制使用 sed
  2. 以尽可能短的方式实现结果,无需管道等。

如果你们中的任何人知道我可能错过的解决方案,我将永远感激任何 URL、指针等。

最佳答案

IIUC,您只需要:

last reboot | sed 's,[()],,g'

关于regex - 在 sed 单行代码中用空格替换/删除 2 个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65654968/

相关文章:

xml - 使用 bash 脚本从 XML 文件中提取特定关键字

shell - 将逗号分隔的单元格(不同条目)分隔到新行

sed - 如何使用 sed 来搜索 ^A 并替换它

python - 使用 Sqlite 在 pandas SQL API 上执行 REGEX 函数时出现导入错误 : Using URI string without sqlalchemy installed,

python - 查找 pandas.Series 中的所有非数字元素

regex - 尝试使用 Sass 编译时出现 "regexp has invalid interval"

bash - 对多种文件类型使用 `find` 并替换找到的文件中的字符串

regex - 是否存在可以从文件中特定的一组行中删除一行的正则表达式,Perl或sed?

html - Perl 正则表达式在在线 PCRE 测试器中工作,但在 perl 命令中不工作

regex - 使用正则表达式验证段落输入中的角度表达式