sed: -e expression #1, char 21: unknown option to

2019-08-29 05:21发布

I am trying to shorten the string in linux and retaining the extension in linux using sed but my command is erroring out with "sed: -e expression #1, char 21: unknown option to `s'"

The string I take is "15400_AP705_OUT_03112016_0513_001.DAT" and the output I am looking for is 15400_AP705.DAT

The command I am using is

echo 15400_AP705_OUT_03112016_0513_001.DAT | sed -r "s//(.{11}).*(\..*)$/$1$2/"

Regards, Srinath

标签: linux sed
1条回答
小情绪 Triste *
2楼-- · 2019-08-29 05:50

There one slash too many in the sed command, you should use single-quotes (not double-quotes) to avoid shell substitutions and use \N for back-references (i.e. not $1 but \1):

echo 15400_AP705_OUT_03112016_0513_001.DAT | sed -r 's/(.{11}).*(\..*)$/\1\2/'

Note that the -roption to sed is not supported on all platforms. On Mac OSX systems, use -E instead.

查看更多
登录 后发表回答