输入(错误地由 sed 脚本处理,请忽略 /\n/)

输入(错误地由 sed 脚本处理,请忽略 /\n/)

因此,我正在指定端口上侦听 netcat 并将输出保存到日志文件中。显然日志文件是 oneliner...我如何正确格式化它?如果您合并 grc 或类似的程序来添加颜色并以某种方式巧妙地删除包含的颜色代码,则额外加分:

输入(错误地由 sed 脚本处理,请忽略 /\n/)

[{"startTime":"1","categoryName":"2","data":"3","level":"4","context":"5","pid":7520},"2021-03-25T08:01:08.086Z","default",["6"],{"level":20000,"levelStr":"7","colour":"8"},{},"Hello, log4js! \n","INFO","green"]__LOG4JS__[{"startTime":"1","categoryName":"2","data":"3","level":"4","context":"5","pid":7520},"2021-03-25T08:01:08.096Z","default",["6"],{"level":30000,"levelStr":"7","colour":"8"},{},"Test warning! This is not a drill! \n","WARN","yellow"]__LOG4JS__[{"startTime":"1","categoryName":"2","data":"3","level":"4","context":"5","pid":7520},"2021-03-25T08:01:08.229Z","default",["6"],{"level":20000,"levelStr":"7","colour":"8"},{}/\\n/,"Connected to mongo","INFO","green"]

输出

[{"startTime":"1","categoryName":"2","data":"3","level":"4","context":"5","pid":7520},
"2021-03-25T08:01:08.086Z","default",["6"],{"level":20000,"levelStr":"7","colour":"8"},{},"Hello, log4js! \n","INFO","green"]__LOG4JS__[{"startTime":"1","categoryName":"2","data":"3","level":"4","context":"5","pid":7520},
"2021-03-25T08:01:08.096Z","default",["6"],{"level":30000,"levelStr":"7","colour":"8"},{},"Test warning! This is not a drill! \n","WARN","yellow"]__LOG4JS__[{"startTime":"1","categoryName":"2","data":"3","level":"4","context":"5","pid":7520},
"2021-03-25T08:01:08.229Z","default",["6"],{"level":20000,"levelStr":"7","colour":"8"},{}/\\n/,"Connected to mongo","INFO","green"]

我尝试完成此任务(请注意这是不正确的)

awk '{gsub(/\\n/,"__LOG4JS__")}1' a="$(ncat -l -k 10.0.0.1 10000)" log.log

答案1

根本不清楚你想要做什么,但如果它只是将每个转换__LOG4JS__为换行符,那么使用 GNU awk for multi-char RS 就可以了(我修复了你的输入以删除/\\n/第三个中存在的虚假内容)行生成无效的 JSON,我认为它实际上并不存在于您的真实数据中):

$ awk -v RS='__LOG4JS__|\r?\n' '1' file
[{"startTime":"1","categoryName":"2","data":"3","level":"4","context":"5","pid":7520},"2021-03-25T08:01:08.086Z","default",["6"],{"level":20000,"levelStr":"7","colour":"8"},{},"Hello, log4js! \n","INFO","green"]
[{"startTime":"1","categoryName":"2","data":"3","level":"4","context":"5","pid":7520},"2021-03-25T08:01:08.096Z","default",["6"],{"level":30000,"levelStr":"7","colour":"8"},{},"Test warning! This is not a drill! \n","WARN","yellow"]
[{"startTime":"1","categoryName":"2","data":"3","level":"4","context":"5","pid":7520},"2021-03-25T08:01:08.229Z","default",["6"],{"level":20000,"levelStr":"7","colour":"8"},{},"Connected to mongo","INFO","green"]

您可以对任何 awk 执行相同的操作,但这会将整个文件读入内存(与等效的 sed 解决方案一样):

awk '{sub(/\r$/,""); gsub(/__LOG4JS__/,ORS)}1' file

如果您尝试做其他事情,请澄清那是什么,并更新问题中的示例以显示预期的输出。

如果您只想查看格式化的 json 输出,请使用上面的代码:

$ awk -v RS='__LOG4JS__|\r?\n' '1' file | jq .
[
  {
    "startTime": "1",
    "categoryName": "2",
    "data": "3",
    "level": "4",
    "context": "5",
    "pid": 7520
  },
  "2021-03-25T08:01:08.086Z",
  "default",
  [
    "6"
  ],
  {
    "level": 20000,
    "levelStr": "7",
    "colour": "8"
  },
  {},
  "Hello, log4js! \n",
  "INFO",
  "green"
]
[
  {
    "startTime": "1",
    "categoryName": "2",
    "data": "3",
    "level": "4",
    "context": "5",
    "pid": 7520
  },
  "2021-03-25T08:01:08.096Z",
  "default",
  [
    "6"
  ],
  {
    "level": 30000,
    "levelStr": "7",
    "colour": "8"
  },
  {},
  "Test warning! This is not a drill! \n",
  "WARN",
  "yellow"
]
[
  {
    "startTime": "1",
    "categoryName": "2",
    "data": "3",
    "level": "4",
    "context": "5",
    "pid": 7520
  },
  "2021-03-25T08:01:08.229Z",
  "default",
  [
    "6"
  ],
  {
    "level": 20000,
    "levelStr": "7",
    "colour": "8"
  },
  {},
  "Connected to mongo",
  "INFO",
  "green"
]

答案2

如果输入是格式良好的 JSON(但事实并非如此),您可以这样做:

ncat ... |  sed -e 's/__LOG4JS__/\n/g' | jq -s -M .

__LOG4JS__是个消息结束分隔符因此sed将其转换为换行符,然后jq 转换为漂亮打印(用于-M抑制颜色转义并仅显示 JSON 格式)。

输入的问题是:{}/\\n/ 无法解析(第三行)。

我不完全确定你想用颜色做什么,这将从输出中完全删除指定的颜色字符串(假设绿色,黄色,红色),并-C着色(通常是终端输出的默认值)。

ncat ... | sed ... | jq -C '. - ["green","yellow","red"]'

可以选择添加-c以压缩输出而不是多行漂亮打印,这将更接近您的示例输出。

(您可能会发现输出的某些部分不可见,具体取决于您的终端背景 - 颜色至少硬编码到 jq-1.5。)

相关内容