我有一个日志文件,其结构如下:
- 时间戳标头
- 日志行
此类文件的示例如下:
Jun 03, 2020 10:39:04 AM pacakge.subpackage.Class method
FINE: ---
Jun 03, 2020 10:39:04 AM pacakge.subpackage.Class method
FINE: index: 14 timestamp: 1,590,170,100 value: 6
Jun 03, 2020 10:39:04 AM pacakge.subpackage.Class method
FINE: delta totalA: 0 total: 5 totalA/total: 0 totalA/total/deltaC.length: 0
Jun 03, 2020 10:39:04 AM pacakge.subpackage.Class method
FINE: bA index: 0 p: 294,325 b: 0 bb: 5 a: 0 aa: 0 total: 5
Jun 03, 2020 10:39:04 AM pacakge.subpackage.Class method
FINE: positionX: 141 positionY: 39
Jun 03, 2020 10:39:04 AM pacakge.subpackage.Class method
FINE: ---
Jun 03, 2020 10:39:04 AM pacakge.subpackage.Class method
FINE: index: 14 timestamp: 1,590,170,100 value: 6
Jun 03, 2020 10:39:04 AM pacakge.subpackage.Class method
FINE: delta totalA: 0 total: 5 totalA/total: 0 totalA/total/deltaC.length: 0
Jun 03, 2020 10:39:04 AM pacakge.subpackage.Class method
FINE: bA index: 0 p: 294,325 b: 0 bb: 5 a: 0 aa: 0 total: 5
Jun 03, 2020 10:39:04 AM pacakge.subpackage.Class method
FINE: positionX: 141 positionY: 39
我想删除除以下时间戳之外的所有时间戳:---
---
Jun 03, 2020 10:39:04 AM pacakge.subpackage.Class method
FINE: index: 14 timestamp: 1,590,170,100 value: 6
FINE: delta totalA: 0 total: 5 totalA/total: 0 totalA/total/deltaC.length: 0
FINE: bA index: 0 p: 294,325 b: 0 bb: 5 a: 0 aa: 0 total: 5
FINE: positionX: 141 positionY: 39
---
Jun 03, 2020 10:39:04 AM pacakge.subpackage.Class method
FINE: index: 14 timestamp: 1,590,170,100 value: 6
FINE: delta totalA: 0 total: 5 totalA/total: 0 totalA/total/deltaC.length: 0
FINE: bA index: 0 p: 294,325 b: 0 bb: 5 a: 0 aa: 0 total: 5
FINE: positionX: 141 positionY: 39
我已经尝试过这个:
cat logFile.txt | awk -F':' '/$2=="---"/ {next; print $0; continue}; !/^FINE/ {next}; {print}'
没有成功。
我正在使用 FreeBSD 12.1(csh,但我想这无关紧要,因为正确的工具是 awk,如果我错了,请纠正我)。
答案1
sed 可以做到:
$ sed -nE '/---/{s/.*: (-*)/\1/;N;p}; /FINE/p' file
---
Jun 03, 2020 10:39:04 AM pacakge.subpackage.Class method
FINE: index: 14 timestamp: 1,590,170,100 value: 6
FINE: delta totalA: 0 total: 5 totalA/total: 0 totalA/total/deltaC.length: 0
FINE: bA index: 0 p: 294,325 b: 0 bb: 5 a: 0 aa: 0 total: 5
FINE: positionX: 141 positionY: 39
---
Jun 03, 2020 10:39:04 AM pacakge.subpackage.Class method
FINE: index: 14 timestamp: 1,590,170,100 value: 6
FINE: delta totalA: 0 total: 5 totalA/total: 0 totalA/total/deltaC.length: 0
FINE: bA index: 0 p: 294,325 b: 0 bb: 5 a: 0 aa: 0 total: 5
FINE: positionX: 141 positionY: 39
如果/---/
模式匹配:
s/.*: (-*)/\1/
=> 将“FINE: ---”替换为“---”
N;p
=> 将当前行附加到下一行(即附加下一个日期戳行)
/FINE/p
=> 所有其他带有 FINE 的行都将被打印。
另一个 awk :
$ awk '/---/ { getline h;$0="---\n"h;print } /^FINE/{print }' file
答案2
$ awk 'p=="FINE: ---" && !/FINE/; {p=$0} /FINE/{sub(/FINE: -/, "-"); print}' ip.txt
---
Jun 03, 2020 10:39:04 AM pacakge.subpackage.Class method
FINE: index: 14 timestamp: 1,590,170,100 value: 6
FINE: delta totalA: 0 total: 5 totalA/total: 0 totalA/total/deltaC.length: 0
FINE: bA index: 0 p: 294,325 b: 0 bb: 5 a: 0 aa: 0 total: 5
FINE: positionX: 141 positionY: 39
---
Jun 03, 2020 10:39:04 AM pacakge.subpackage.Class method
FINE: index: 14 timestamp: 1,590,170,100 value: 6
FINE: delta totalA: 0 total: 5 totalA/total: 0 totalA/total/deltaC.length: 0
FINE: bA index: 0 p: 294,325 b: 0 bb: 5 a: 0 aa: 0 total: 5
FINE: positionX: 141 positionY: 39
{p=$0}
这将当前行保存在p
变量中p=="FINE: ---" && !/FINE/
这会检查上一行是否FINE: ---
与当前行不匹配FINE
。如果满足条件,将打印当前行/FINE/{sub(/FINE: -/, "-"); print}
如果当前行包含FINE
,该函数将从行中sub
删除。然后打印该行FINE:
FINE: ---
您还可以使用:
awk 'p; {p=0} /FINE/{if($2=="---") {print $2; p=1} else print}'
# or using getline, assuming no errors
awk '/FINE/{if($2=="---") {print $2; getline} print}'
答案3
$ awk '/^FINE:/{print (/---$/ ? $2 ORS p : $0)} {p=$0}' file
---
Jun 03, 2020 10:39:04 AM pacakge.subpackage.Class method
FINE: index: 14 timestamp: 1,590,170,100 value: 6
FINE: delta totalA: 0 total: 5 totalA/total: 0 totalA/total/deltaC.length: 0
FINE: bA index: 0 p: 294,325 b: 0 bb: 5 a: 0 aa: 0 total: 5
FINE: positionX: 141 positionY: 39
---
Jun 03, 2020 10:39:04 AM pacakge.subpackage.Class method
FINE: index: 14 timestamp: 1,590,170,100 value: 6
FINE: delta totalA: 0 total: 5 totalA/total: 0 totalA/total/deltaC.length: 0
FINE: bA index: 0 p: 294,325 b: 0 bb: 5 a: 0 aa: 0 total: 5
FINE: positionX: 141 positionY: 39
答案4
这是在 freenode 中建议的,我认为它很优雅并且更容易阅读:
awk '$1 != "FINE:" {h=$0} $2 == "---" {printf "---\n%s\n", h} $1 == "FINE:" && $2 != "---"'