如何使用 awk 或 sed 将 mysql.log 重新格式化为简单格式?
我有一段mysql.log:
131024 13:17:40 1 Query select * from test_numbers
1 Query select
n.id,
n.name,
n.value
from test_numbers AS n
limit 0, 50
1 Query select
count(*)
FROM test_numbers
1 Query SHOW STATUS
131024 13:17:50 1 Query SHOW STATUS
131024 13:18:00 1 Query select * from test_numbers
1 Query select
n.id,
n.name,
n.value
from test_numbers AS n
limit 0, 50
1 Query select
count(*)
FROM test_numbers
1 Query SHOW STATUS
“查询”行跨越多行。我想重新格式化为这种格式:
131024 13:17:40 1 Query select * from test_numbers
131024 13:17:40 1 Query select n.id, n.name, n.value from test_numbers AS n limit 0, 50
131024 13:17:40 1 Query select count(*) FROM test_numbers
131024 13:17:40 1 Query SHOW STATUS
131024 13:17:50 1 Query SHOW STATUS
131024 13:18:00 1 Query select * from test_numbers
131024 13:18:00 1 Query select n.id, n.name, n.value from test_numbers AS n limit 0, 50
131024 13:18:00 1 Query select count(*) FROM test_numbers
131024 13:18:00 1 Query SHOW STATUS
当多行“查询”连接到 1 行时。
我尝试了一些脚本,但失败了:
sed -r ':a;N;$!ba;s/\n(^(([0-9]+\t[0-9:]+)|(\t\t[0-9:]+)))/ \1/g' mysql.log
awk '/^(([0-9]+ [0-9:]+)|(\t\t[0-9:]+))/{print "";next}{printf $0}END{print "";}' mysql.log
谢谢!我修改了一些贡献。请查看发布的答案。