使用 sed 在 '=' 之后返回值
一直在阅读 Unix 中的 sed 命令,我认为它会做我想要的事情,只是不确定它的疯狂语法。
基本上,我只想获取文件最后一行中 Count= 后面括号内的值。因此,该行中将有一个 Count=[#]
并且我只想返回 #。有什么想法吗? sed 是最好的选择吗(听说 awk 可能会做类似的事情)?
been reading up on the sed command in Unix and I think it will do what I want it to, just not sure on its crazy syntax.
Basically, I want to get just the value inside brackets after Count= in the last line of a file. So, in the line there will be a Count=[#]
and I want just the # to be returned. Any thoughts? Is sed even the best choice (heard awk might do something similar)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
稍微不同的方法:
这匹配
[
和]
之间的文本,并用它替换整行。A slightly different approach:
This matches the text between the
[
and]
, and replaces the entire line with it.还有另一种方法:
-n
不打印,除非使用p
。$
表示最后一行。s/search/replace/
搜索将被替换。\([^]]\+\)
捕获一个或多个出现的 not ']'^]
。\1
指的是捕获的内容。p
打印行。考虑到任何行中可能出现 Count=[#] ,我所看到的这是唯一的答案。您声明这仅发生在最后一行:“在文件最后一行的 Count= 之后。”
Yet another approach:
-n
don't print except withp
.$
for the last row.s/search/replace/
search will be replaced by replace.\([^]]\+\)
capture one or more occurrence of not ']'^]
.\1
is referring to what was captured.p
print line.What I can see this is the only answer taking into account that there might come Count=[#] in any row. You state that this is to happen only for the last row: ' after Count= in the last line of a file.'
使用 sed,只需使用几个正则表达式来删除您不需要的部分:
或者
第一个表达式将删除
Count=[
,第二个表达式将删除尾随的].
With sed, just use a couple of regular expressions to remove the part you don't want:
or
The first expression will remove
Count=[
and the second expression will remove the trailing]
.