[英]Bash regex with sed
Trying to get only words that come before "/", I wrote:试图只获取“/”之前的单词,我写道:
T='He/She is a very handsome/beautiful man/woman indeed.'
echo "$T" | sed -E 's#\b(.*)/(.*)\b#\1#g'
However, I only got to make it work in the last occurrence (although I'm using "g" in sed sentence):但是,我只需要在最后一次出现时让它工作(尽管我在 sed 句子中使用了“g”):
He/She is a very handsome/beautiful man.
My desired output is: "He is a very handsome man indeed."我想要的输出是:“他确实是一个非常英俊的人。”
Any ideas?有任何想法吗? Thank you.谢谢你。
A POSIX compliant one:一个符合 POSIX 的:
T='He/She is a very handsome/beautiful man/woman indeed.'
echo "$T" | sed 's:/[^ ]*::g'
To remove non-space characters after every slash you can use:要在每个斜杠后删除非空格字符,您可以使用:
$ sed 's:/\S*::g' <<<'He/She is a very handsome/beautiful man/woman indeed.'
He is a very handsome man indeed.
The pattern, :/\\S*:
, matches a slash followed by zero or more non-space characters.模式:/\\S*:
匹配后跟零个或多个非空格字符的斜杠。 The replacement string, ::
, is empty and it is applied globally g
.替换字符串::
为空,它被全局应用g
。 The<<<
is a here-string that passes input to sed. <<<
是将输入传递给 sed 的此处字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.