bash - 有没有办法从日志中提取一个值并进一步使用它来使用 bash 提取另一个值
问题描述
我正在尝试从日志文件中读取一个值,然后根据该值搜索另一个文本。
下面是我的日志文件的样子。我只有 customerId ,订单 ID 是动态生成的
我想首先根据 customerId 捕获 orderId 并存储在变量中。
一旦成功,我想检查这个订单 ID 的状态,它是下面大约 10 行
最后,在控制台打印或写入文件都没关系
2019-05-18 09:46:02.944 [thread-2 ] Orderprocesing: Sending order info '{
"customerName" : "jenny",
"customerId" : "JE19802501",
"customerphone" : null,
"orderId" : "8456/72530548",
"orderInfo" : {
"Item" : "comic series 2018",
"count" : "10"
}
}'
.............................................................
.............................................................
2019-05-18 09:46:02.944 [thread-2 ] Orderprocesing: Sending order info '{
"customerName" : "jenny",
"customerId" : "JE19802501",
"customerphone" : null,
"orderId" : "8456/82530548",
"orderInfo" : {
"Item" : "comic series 2019",
"count" : "10"
}
}'
.............................................................
.............................................................
2019-05-18 09:49:02.944 [thread-2 ] Orderprocesing: status for 82530548 is success
.............................................................
.............................................................
.............................................................
2019-05-18 09:50:06.872 [thread-2 ] Orderprocesing: status for 72530548 is success
我是新 bash,我设法将包含与 CustomerID 对应的 OrderId 的 10 行切片,但无法切片 OrderId 并将其存储在变量中
$ cat orderlog_may_18 grep -A 15 "JE19802501"
预期的结果是打印
customerId : JE19802501
orderId : 72530548
status for 72530548 is success
customerId : JE19802501
orderId : 82530548
status for 82530548 is success
解决方案
两行 bash,使用 sed。
ord=$(sed -n '/JE19802501/,/orderId/{/orderId/{s/[^0-9]//gp}}' orderlog_may18)
sed -n "/status for $ord/s/.*://p" orderlog_may18
$ord 存储 JE198002501 之后 orderId 行中的数字。然后打印状态行的尾端。
您应该能够在 bash 脚本中进行所需的格式设置。
推荐阅读
- rust - 为什么我可以在共享引用上调用 Write::write_all,即使它需要可变引用?
- apollo - 如何制作阿波罗服务器预处理器
- javascript - 如何在 c3.js 中使用多个 XY 折线图构造多个数据数组
- vue.js - 服务堆栈 Vue SPA 应用程序
- linux - 有没有办法默认 Go 的 os 包调用在特定用户下运行
- python-3.x - 如何解决使用 sqlalchemy 连接执行 pandas.to_sql() 时遇到的 InvalidRequestError?
- python - Python 在游戏中找不到 SSL 模块但在电脑中找不到问题
- r - 如何在闪亮的上下文中更改绘图的高度
- prolog - `nonvar(X)` 和 `\+var(X)` 之间有区别吗?
- toml - 数组内的 TOML 多级表语法——非法与否