【发布时间】:2019-07-05 04:09:52
【问题描述】:
我正在尝试删除文本字符串中的每个字符,除了剩余的 11 个字符。字符串是Sample Text_that-would$normally~be,here--pe_-l4_mBY,而我想要得到的只是-pe_-l4_mBY。
这是我尝试过的:
$ cat food
Sample Text_that-would$normally~be,here--pe_-l4_mBY
$ cat food | sed 's/^.*(.{3})$/\1/'
sed: 1: "s/^.*(.{3})$/\1/": \1 not defined in the RE
请注意,文本字符串并没有真正存储在文件中,我只是以cat food 为例。
操作系统是 macOS High Sierra 10.13.6,bash 版本是 3.2.57(1)-release
【问题讨论】:
-
改进的示例,没有意识到我放了 10 个字符而不是 11 个:-/
-
如果您需要 BRE POSIX 模式,请使用
cat food | sed 's/^.*\(.\{11\}\)$/\1/'。
标签: bash macos unix sed text-processing