sed - 从字符串中删除特定的下标
请向我提供一个 sed oneliner,它提供以下输出: sdc3 sdc2 对于输入: sdc3[1] sdc2[0]
我的意思是从字符串中删除所有下标值..
please provide me a sed oneliner which provides this output:
sdc3 sdc2
for Input :
sdc3[1] sdc2[0]
I mean remove all subscript value from the string ..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
读取:用文字“[”替换任何字符串,后跟零个或多个不是“]”的字符,然后用空字符串替换结束的“]”。
您需要
[^]]
位来防止贪婪匹配将“[1] sdc2[0]”视为示例字符串中的单个匹配项。至于您的评论:
/dev/
位(我不会说“为了清楚起见”)\(...\)
位匹配一个子组,这里是 sdc2 或其他,所以我们可以在替换中引用它,g
标志告诉它每行执行多个匹配,而不是在第一个匹配处停止reads: substitute any string with literal "[" followed by zero or more characters that aren't a "]", and then the closing "]", with an empty string.
You need the
[^]]
bit to prevent greedy matching treating "[1] sdc2[0]" as a single match in your sample string.As for your comment:
/dev/
bit you asked for (I won't say "for clarity")\(...\)
bit matches a subgroup, here sdc2 or whatever, so we can refer to it in the replacement[^[ ]
means any character except an "[" (again, to avoid greedily matching the index) or a space (assuming your values are space-delimited as per your post)g
flag at the end tells it to perform multiple matches per line, instead of stopping at the first one