如果grep报告相同模式的多个实例,如何添加空行?

时间:2018-08-27 17:49:00

标签: bash grep

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 ..."}}}

3 个答案:

答案 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