sed:删除除最后 n 个字符以外的所有字符

lee*_*oon 2 unix macos bash text-processing sed

我试图删除文本字符串中除剩余 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
Run Code Online (Sandbox Code Playgroud)

请注意,文本字符串并没有真正存储在文件中,我只是用作cat food示例。

操作系统为 macOS High Sierra 10.13.6,bash版本为 3.2.57(1)-release

anu*_*ava 5

您可以将其sed与捕获组一起使用:

sed -E 's/.*(.{11})$/\1/' file
Run Code Online (Sandbox Code Playgroud)

-pe_-l4_mBY
Run Code Online (Sandbox Code Playgroud)