如何使用SED使用字符串作为搜索参数提取字符串
我试图返回一个通过输出不断变化的字符串。 我认为 grep 和 sed 可能是处理这个问题的最佳方法。 基本上,输出呈现一个大的文本字段,其中包含一些内容,目标
https://rancher.website.com/version1/proj/abcd123?somethingsomethingsomethingetc
是返回第一个实例,
https://rancher.website.com/version1/proj/
因为有多个实例,然后返回 abcd123
的内容,直到 ?
符号之前。
我一直在尝试调整 sed 命令,但我的语法不断出错,而且我很难理解到目前为止给出的解释。我认为我的问题之一可能是因为 sed “搜索”缺乏更好的术语,是针对字符串而不是单个字符运行的。另外,正斜杠也是一个问题,因为我认为它们在 sed 命令中被解释为运算符?
我尝试使用的命令或多或少是:
grep 'https://rancher.website.com/version1/projects/' grep.txt | sed 's/^.*'https:\/\/rancher.website.com\/version1\/projects\/'//g'
grep.txt 是初始命令的输出文本的容器
I am trying to return a string that is constantly changing via the output.
I decided grep and sed might be the best way to handle this.
Basically the output renders a large field of text that contains something to the effect of
https://rancher.website.com/version1/proj/abcd123?somethingsomethingsomethingetc
the goal is to return the FIRST instance of
https://rancher.website.com/version1/proj/
as there are multiple, and then return the contents of abcd123
up to the ?
symbol before it.
I keep trying to tweak the sed command but keep running into errors with my syntax and I am having trouble wrapping my mind around the explanations given so far. I think one of my problems might be around the fact that the sed "search" for lack of a better term, is being run against a string rather than a single character. Also the forward slashes are a problem as I think those are being interpreted as operators in the sed command?
Command I am trying to use is more or less:
grep 'https://rancher.website.com/version1/projects/' grep.txt | sed 's/^.*'https:\/\/rancher.website.com\/version1\/projects\/'//g'
With grep.txt being the container for the output text of the initial command
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将最后一个
/
之前的所有内容以及?
中的所有内容替换为空字符串。当模式包含
/
时,请使用不同的字符作为s
分隔符以避免必须对其进行转义;我在上面使用了#
。Replace everything up to the last
/
and everything from?
with empty strings.When the pattern includes
/
, use a different character as thes
delimiter to avoid having to escape it; I've used#
above.