如何更改如下所示的日志文件:
Jul 6 17:35:10 sdc-prius motion_planner[1284]: new destination: [55.733510, 37.587401]
Jul 6 17:35:11 sdc-prius control[1284]: next waypoint: [55.733668, 37.587143]
Jul 6 17:35:11 sdc-prius control[1281]: steering: 212, throttle: 420
Jul 6 17:35:11 sdc-prius control[1281]: steering: 145, throttle: 634
Jul 6 17:35:12 sdc-prius control[1281]: steering: 65, throttle: 538
Jul 6 17:35:13 sdc-prius control[1284]: next waypoint: [55.733948, 37.586731]
Jul 6 17:35:13 sdc-prius control[1281]: steering: 121, throttle: 340
Jul 6 17:35:13 sdc-prius control[1281]: steering: 150, throttle: 600
Jul 6 17:35:14 sdc-prius control[1281]: steering: 46, throttle: 346
Jul 6 17:35:13 sdc-prius control[1284]: next waypoint: [55.733955, 37.586744]
Jul 6 17:35:13 sdc-prius control[1281]: steering: 485, throttle: 567
Jul 6 17:35:13 sdc-prius control[1284]: next waypoint: [55.734094, 37.586518]
Jul 6 17:35:14 sdc-prius control[1281]: steering: 278, throttle: 400
Jul 6 17:35:14 sdc-prius control[1284]: next waypoint: [55.734300, 37.586229]
Jul 6 17:35:14 sdc-prius control[1281]: steering: 982, throttle: 400
Jul 6 17:35:14 sdc-prius control[1284]: next waypoint: [55.734530, 37.585896]
Jul 6 17:35:14 sdc-prius control[1281]: steering: 98, throttle: 400
Jul 6 17:35:15 sdc-prius control[1284]: next waypoint: [55.733948, 37.586731]
Jul 6 17:35:15 sdc-prius control[1281]: steering: 33, throttle: 200
Jul 6 17:35:15 sdc-prius motion_planner[1284]: new destination: [55.734839, 37.585531]
Jul 6 17:35:15 sdc-prius control[1281]: steering: 2, throttle: 100
但需要像:
Jul 6 17:35:11 37.587143, 55.733668
Jul 6 17:35:13 37.586731, 55.733948
Jul 6 17:35:13 37.586744, 55.733955
Jul 6 17:35:13 37.586518, 55.734094
Jul 6 17:35:14 37.586229, 55.734300
Jul 6 17:35:14 37.585896, 55.734530
Jul 6 17:35:15 37.586731, 55.733948
我已经能够做到:
grep -o '^[A-Z][a-z][a-z]\s\d\s\d\d\:\d\d\:\d\d\|\[\d\d\.\d\d\d\d\d\d\,\s\d\d\.\d\d\d\d\d\d\]' test.txt
答案1
由于字段的重新排序,您将无法使用 执行此操作grep
,但sed
可以完成工作。
假设您已经对正则表达式有了合理的掌握,那么您应该发现标记源字段并在替换中重新排序它们很简单,尽管sed
无法识别\d
匹配数字。
以下简化的匹配适用于您的测试数据:
sed -En 's/^(.*) sdc-.*\[.*\].*\[(.*), (.*)\]/\1 \3, \2/p' test.txt
(
和分隔字段)
,其中\N
(其中N
到1
)9
表示替换字符串中的字段。
这是 的一个很好的介绍sed
。