Ubuntu 16.04
Bash 4.4.0
我正在使用grep
在作为日志文件的json文件中搜索单词“错误”。如何在每个实例之后添加一个空行?
我的命令:grep error "${wDir}"/"${client}"/logs/server.json >> "$eLog"
输出:
{"name":"XXX_XXX","hostname":"xxx.xx.xxx","pid":5193,"level":30,"fbresponse":{"error":{"message":"(#200) User does not ....."}}}
{"name":"XXX_XXX","hostname":"xxx.xx.xxx","pid":5193,"level":30,"fbresponseraw":{"error":{"message":"(#200) User does not ..."}}}
{"name":"XXX_XXX","hostname":"xxx.xx.xxx","pid":5193,"level":30,"fbresponse":{"error":{"message":"(#200) User does not ....."}}}
{"name":"XXX_XXX","hostname":"xxx.xx.xxx","pid":5193,"level":30,"fbresponseraw":{"error":{"message":"(#200) User does not ..."}}}
所需的输出:
{"name":"XXX_XXX","hostname":"xxx.xx.xxx","pid":5193,"level":30,"fbresponse":{"error":{"message":"(#200) User does not ....."}}}
{"name":"XXX_XXX","hostname":"xxx.xx.xxx","pid":5193,"level":30,"fbresponseraw":{"error":{"message":"(#200) User does not ..."}}}
{"name":"XXX_XXX","hostname":"xxx.xx.xxx","pid":5193,"level":30,"fbresponse":{"error":{"message":"(#200) User does not ....."}}}
{"name":"XXX_XXX","hostname":"xxx.xx.xxx","pid":5193,"level":30,"fbresponseraw":{"error":{"message":"(#200) User does not ..."}}}
答案 0 :(得分:1)
您可以使用awk
进行搜索并插入空行:
awk '/error/ { print $0 ORS }' "${wDir}"/"${client}"/logs/server.json
默认情况下,ORS
(输出记录分隔符)为\n
。
答案 1 :(得分:1)
简单好。
"Serilog": {
"Using": [ "Serilog.Sinks.Console" ],
"MinimumLevel": "Debug",
"WriteTo": [
{
"Name": "RollingFile",
"IsJson": true,
"Args": {
"pathFormat": "C:\\Logs\\Log-{Hour}.json",
"formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog",
"restrictedToMinimumLevel": "Warning"
}
},
{
"Name": "Console"
}
]
},
或者如果您希望它不区分大小写
sed '/error/G' "${wDir}"/"${client}"/logs/server.json >> "$eLog"
示例:
sed '/error/IG' "${wDir}"/"${client}"/logs/server.json >> "$eLog"
答案 2 :(得分:0)
使用grep
:
grep "aa" a.txt | xargs printf "%s\n\n"
输出:
aa
aa
aa
aa