击败SED替换为第一次驱动到右
我试图在之前获得第一个单词/
我正在使用以下sed:
echo 'a/b/c' | sed 's/\(.*\)\/\(.*\)/\1/g'
但是这给了我a/b
,我希望它只给我a
有什么想法吗?
I am trying to obtain the first word before /
I am using following sed:
echo 'a/b/c' | sed 's/\(.*\)\/\(.*\)/\1/g'
But this gives me a/b
, I would like it to give me only a
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
详细信息:
\([^/]*\)
- 组1(\ 1
):/
/
- a/
char。*
- 字符串的其余部分。或者,如果您有变量,则可以使用字符串操作:
在此,
%%
从末端删除最长的子字符串,与/*
glob模式匹配,直到字符串中的第一个/
。You can use
Details:
\([^/]*\)
- Group 1 (\1
): any zero or more chars other than/
/
- a/
char.*
- the rest of the string.Or, if you have a variable, you can use string manipulation:
Here,
%%
removes the longest substring from the end, that matches the/*
glob pattern, up to the first/
in the string including it.可以在
bash
本身中轻松完成此操作,而无需调用任何外部实用程序:This can be done easily in
bash
itself without calling any external utility:使用
sed
和替代定界符,以防止与数据中的类似字符发生冲突。Using
sed
and an alternate delimiter to prevent a conflict with a similar char in the data.使用
剪切
而不是sed
以隔离char分离字段,以清晰,简单和效率:Use
cut
instead ofsed
to isolate char-separated fields for clarity, simplicity, and efficiency: