首页 > 系统相关 >shell脚本将log文件格式化为markdown排版格式

shell脚本将log文件格式化为markdown排版格式

时间:2023-01-06 18:45:27浏览次数:78  
标签:info 27 log 22 38 content shell 文件格式 date

原文地址:https://www.cnblogs.com/liqinglucky/p/format2markdown.html

通常日志文件往往有几十万行,导致查看日志很不方便。利用shell脚本识别日志文件中的关键词并给日志文件加上特定markdown格式的标题段落。

sed命令

在上一行插入内容

sed -i /{关键词}/i\{插入的关键词} {file}

在下一行插入内容

sed -i /{关键词}/a\{插入的关键词} {file}

演示

文件内容如下

文件myfile.md

date:2022-12-27 time:22:38:29.309319 info: title_1
date:2022-12-27 time:22:38:34.744606 info: content_start
date:2022-12-27 time:22:38:34.749685 info: content_end
date:2022-12-27 time:22:38:54.744975 info: title_1
date:2022-12-27 time:22:38:54.752861 info: content_start
date:2022-12-27 time:22:38:59.307548 info: content_end

根据sed命令写成脚本fmt.sh

#!/bin/bash

# 1 在关键词 "title_1"上一行插入 "# title"
sed -i /title_1/i\#\ \title myfile.md
# 2 在关键词 "content_start"上一行插入 "```"
sed -i /content_start/i\`\`\` myfile.md
# 3 在关键词 "content_end"下一行插入 "```"
sed -i /content_end/a\`\`\` myfile.md
echo "format done"

运行

$ ./fmt.sh myfile.md

处理后的文件markdown格式

# title
date:2022-12-27 time:22:38:29.309319 info: title_1
​```
date:2022-12-27 time:22:38:34.744606 info: content_start
date:2022-12-27 time:22:38:34.749685 info: content_end
​```
# title
date:2022-12-27 time:22:38:54.744975 info: title_1
​```
date:2022-12-27 time:22:38:54.752861 info: content_start
date:2022-12-27 time:22:38:59.307548 info: content_end
​```

标签:info,27,log,22,38,content,shell,文件格式,date
From: https://www.cnblogs.com/liqinglucky/p/format2markdown.html

相关文章