【问题标题】:Printing a substring from log从日志打印子字符串
【发布时间】:2016-12-11 09:27:47
【问题描述】:

我有以下格式的日志行:

2016-08-04 19:12:02,537 INFO  ...<Thread-4> - Got a message [......|clientTradeId=xxxxxxx|timeInForce=xxxx|.....TradeResponseMessage]

我需要提取所有带有“收到消息”关键字的行;然后仅打印出最终候选清单中的“clientTradeId=xxxxxxx”部分。

如何通过脚本实现这一点(grep 和 cut?- 或者有更好的选择)

【问题讨论】:

  • 目标是您添加一些自己的代码,以至少显示您为解决这个问题所做的研究工作。

标签: shell sed grep cut


【解决方案1】:

更新命令感谢@BenjaminW:

sed -rn 's/.*(clientTradeId=[0-9]*).*/p' file

没用过sed,但是我用过regex。

查看 sed 的文档

cat file | sed '/.*(clientTradeId=[0-9]*).*/\1/'

它的作用是将文件通过管道传输到 sed,然后使用正则表达式,选择你想要的部分,然后输出它(我希望如此)。

【讨论】:

  • 这应该是一个替换 (s///) - 就像现在一样,它将选择匹配 .*(clientTradeId=[0-9]*).* 的行,然后在它们上运行命令 \1/,这不起作用.此外,您必须将\(\) 用于捕获组,或-r 选项(-E 用于BSD sed)才能使用()。最后,不需要catsed '&lt;command&gt;' file 是一样的,但不会产生子shell。
  • @Cyrus 我对回答问题很陌生。感谢您的链接。
  • @BenjaminW。感谢现场我会改变我的答案
【解决方案2】:

考虑数据在文件data.log中

grep -F "Got a message" data.log | grep -Po "clientTradeId=[^| ]+"

使用剪切

grep -F "Got a message" data.log |  cut -f2 -d'|'

【讨论】:

  • 可以用单个 grep 本身完成 grep -oP 'Got a message.*\KclientTradeId[^|]+'
猜你喜欢
  • 2018-09-24
  • 1970-01-01
  • 2019-06-13
  • 1970-01-01
  • 1970-01-01
  • 2015-07-25
  • 2014-12-26
  • 1970-01-01
  • 2019-03-01
相关资源
最近更新 更多