解析字符串以获取所需数据
我正在尝试解析文件中包含“ID”和数字条目的行。然而,下面的脚本所做的是获取“ID”数值及其后面的所有内容。我怎样才能把它减少为“ID”+数值而不是其他?
提前致谢。
tail -n 1 events.log | sed 's/.*id=\([^)]\+\).*/\1/' > event_id.dat
I'm trying to parse a line within a file that contains "ID" and a numeric entry. However, what the script below is doing is grabbing the "ID" numeric value plus everything after it. How can I just cut it down to "ID" + numeric value and nothing else?
Thanks in advance.
tail -n 1 events.log | sed 's/.*id=\([^)]\+\).*/\1/' > event_id.dat
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
括号中的正则表达式字符串是
[^)]\+
,这意味着“除结束括号之外的所有字符”。如果您想捕获数字,则需要将其更改为
[0-9]\+
Your regex string in brackets is
[^)]\+
, which means "all characters other than the end bracket".If numerical digits is what you want to catch, you need to change that to
[0-9]\+
如果没有输入和预期输出的示例,很难说出您正在寻找的内容,但这可能最普遍:这
相当于:
[0- 9]*
),前后可以有任意数量的内容\1
引用匹配表达式中括号内的部分)想?如果没有,您能否更明确地说明您的输入/输出要求?
It's tough to tell what you're looking for without an example of input and expected output, but this might work most generally:
That amounts to:
[0-9]*
), with any amount of anything before or after\1
references the part within parenthesis in the match expression)Does that do what you want? If not, can you be more explicit with your input/output requirements?