有一个来自成像会话的日志文件,我需要从中提取位于某个触发器之前的多行参数。一旦找到触发器,我就想打印收集的数据并重新开始。
使用 grep,我提取了触发器之前的行。触发器应为“自动对焦完成”。这是我得到的结果,并希望进一步将其输入到 awk 中进行参数提取和打印。
$ grep -B4 'AutoFocus completed' 20240831-195957-3.1.1.9001.10824-*.log
20240831-195957-3.1.1.9001.10824-202408.log-2024-08-31T20:19:16.2135|INFO|FocuserVM.cs|MoveFocuserInternal|212|Moving Focuser to position 21611
20240831-195957-3.1.1.9001.10824-202408.log-2024-08-31T20:19:20.4192|INFO|CameraVM.cs|Capture|737|Starting Exposure - Exposure Time: 5s; Filter: ; Gain: 100; Offset 50; Binning: 1x1;
20240831-195957-3.1.1.9001.10824-202408.log-2024-08-31T20:19:27.9594|INFO|StarDetection.cs|Detect|244|Average HFR: 2.545578870303149, HFR σ: 0.3919148934340784, Detected Stars 609, Sensitivity High, ResizeFactor: 0.33
20240831-195957-3.1.1.9001.10824-202408.log-2024-08-31T20:19:27.9713|INFO|FocuserMediator.cs|BroadcastSuccessfulAutoFocusRun|45|Autofocus notification received - Temperature 28.5
20240831-195957-3.1.1.9001.10824-202408.log:2024-08-31T20:19:27.9719|INFO|AutoFocusVM.cs|StartAutoFocus|277|AutoFocus completed
--
20240831-195957-3.1.1.9001.10824-202408.log-2024-08-31T20:55:18.0098|INFO|FocuserVM.cs|MoveFocuserInternal|212|Moving Focuser to position 21573
20240831-195957-3.1.1.9001.10824-202408.log-2024-08-31T20:55:22.1916|INFO|CameraVM.cs|Capture|737|Starting Exposure - Exposure Time: 5s; Filter: ; Gain: 100; Offset 50; Binning: 1x1;
20240831-195957-3.1.1.9001.10824-202408.log-2024-08-31T20:55:29.6817|INFO|StarDetection.cs|Detect|244|Average HFR: 2.4623768995206703, HFR σ: 0.37028813097450053, Detected Stars 832, Sensitivity High, ResizeFactor: 0.33
20240831-195957-3.1.1.9001.10824-202408.log-2024-08-31T20:55:29.6834|INFO|FocuserMediator.cs|BroadcastSuccessfulAutoFocusRun|45|Autofocus notification received - Temperature 27.6
20240831-195957-3.1.1.9001.10824-202408.log:2024-08-31T20:55:29.6839|INFO|AutoFocusVM.cs|StartAutoFocus|277|AutoFocus completed
--
20240831-195957-3.1.1.9001.10824-202408.log-2024-08-31T22:01:56.2682|INFO|FocuserVM.cs|MoveFocuserInternal|212|Moving Focuser to position 21589
20240831-195957-3.1.1.9001.10824-202408.log-2024-08-31T22:02:00.4053|INFO|CameraVM.cs|Capture|737|Starting Exposure - Exposure Time: 5s; Filter: ; Gain: 100; Offset 50; Binning: 1x1;
20240831-195957-3.1.1.9001.10824-202408.log-2024-08-31T22:02:08.0464|INFO|StarDetection.cs|Detect|244|Average HFR: 2.337959785686696, HFR σ: 0.43475116635610034, Detected Stars 972, Sensitivity High, ResizeFactor: 0.33
20240831-195957-3.1.1.9001.10824-202408.log-2024-08-31T22:02:08.0476|INFO|FocuserMediator.cs|BroadcastSuccessfulAutoFocusRun|45|Autofocus notification received - Temperature 26
20240831-195957-3.1.1.9001.10824-202408.log:2024-08-31T22:02:08.0477|INFO|AutoFocusVM.cs|StartAutoFocus|277|AutoFocus completed
我想要捕捉:
- “移动调焦器到位置”后面的数字
- “平均 HFR:”后面的数字
- “已探测到的星星”后面的数字
一旦我遇到“自动对焦完成”,我想打印在 .log 和 |INFO 之间的行部分中编码的时间和日期,然后是位置 hfr 和星星
我尝试了部分代码,但未能打印出 hfr 和日期
$ grep -B4 'AutoFocus completed' 20240831-195957-3.1.1.9001.10824-*.log \
| awk '/Moving Focuser to/ { pos= $5 }; /Average HFR/ { hfr=$3}; /AutoFocus completed/ { print $1,pos,hfr }'
2.545578870303149,.1.9001.10824-202408.log:2024-08-31T20:19:27.9719|INFO|AutoFocusVM.cs|StartAutoFocus|277|AutoFocus 21611
2.4623768995206703,1.9001.10824-202408.log:2024-08-31T20:55:29.6839|INFO|AutoFocusVM.cs|StartAutoFocus|277|AutoFocus 21573
2.337959785686696,.1.9001.10824-202408.log:2024-08-31T22:02:08.0477|INFO|AutoFocusVM.cs|StartAutoFocus|277|AutoFocus 21589
2.360274405865274,.1.9001.10824-202408.log:2024-08-31T23:09:24.1184|INFO|AutoFocusVM.cs|StartAutoFocus|277|AutoFocus 21558
2.3450609716393003,1.9001.10824-202409.log:2024-09-01T00:16:57.9708|INFO|AutoFocusVM.cs|StartAutoFocus|277|AutoFocus 21566
2.361727006131884,.1.9001.10824-202409.log:2024-09-01T01:09:04.0561|INFO|AutoFocusVM.cs|StartAutoFocus|277|AutoFocus 21568
2.561488030855142,.1.9001.10824-202409.log:2024-09-01T02:12:22.2001|INFO|AutoFocusVM.cs|StartAutoFocus|277|AutoFocus 21561
2.3263163204824595,1.9001.10824-202409.log:2024-09-01T03:19:34.1076|INFO|AutoFocusVM.cs|StartAutoFocus|277|AutoFocus 21610
2.371915558447258,.1.9001.10824-202409.log:2024-09-01T04:26:36.0461|INFO|AutoFocusVM.cs|StartAutoFocus|277|AutoFocus 21588
期望输出:
2024-08-31T20:19:27.9719 21611 2.545578870303149 609
2024-08-31T20:55:29.6839 21573 2.4623768995206703 832
2024-08-31T22:02:08.0477 21589 2.337959785686696 972
在 WSL 中运行 awk。
awk --version
GNU Awk 5.1.0, API: 3.0 (GNU MPFR 4.1.0, GNU MP 6.2.1)
Copyright (C) 1989, 1991-2020 Free Software Foundation.
更新以解决 Ed Morton 的回答
awk 脚本仅与前面的 grep 配合使用。似乎模式的某些部分只与 grep 前面的文件名挂钩。没有 grep 则不起作用。不过这是完全可以接受的,我会将其标记为选定的答案。
这有效:
grep -B4 'AutoFocus completed' 20240831-195957-3.1.1.9001.10824-*.log | awk -f AF_stats.awk
2024-08-31T20:19:27.9719 21611 2.545578870303149 609
2024-08-31T20:55:29.6839 21573 2.4623768995206703 832
2024-08-31T22:02:08.0477 21589 2.337959785686696 972
2024-08-31T23:09:24.1184 21558 2.360274405865274 999
2024-09-01T00:16:57.9708 21566 2.3450609716393003 1076
2024-09-01T01:09:04.0561 21568 2.361727006131884 1067
2024-09-01T02:12:22.2001 21561 2.561488030855142 1017
2024-09-01T03:19:34.1076 21610 2.3263163204824595 1021
2024-09-01T04:26:36.0461 21588 2.371915558447258 1008
这不起作用:
cat 20240831-195957-3.1.1.9001.10824-*.log | awk -f AF_stats.awk