AWK 将一些特殊的新行替换为一行

AWK 将一些特殊的新行替换为一行

我有一些像这样的日志:

2023-11-15T08:59:28.000000+00:00 database-1 # Time: 231115  8:59:28
# User@Host: rdsadmin[rdsadmin] @ localhost []
# Thread_id: 3  Schema:   QC_hit: No
# Query_time: 0.000123  Lock_time: 0.000000  Rows_sent: 1  Rows_examined: 0
# Rows_affected: 0  Bytes_sent: 49
SET timestamp=1700038768;
SET STATEMENT max_statement_time=60 FOR SELECT 1;

您能帮助我将使用 awk 或类似的东西改为:

2023-11-15T08:59:28.000000+00:00 database-1 # Time: 231115  8:59:28 # User@Host: rdsadmin[rdsadmin] @ localhost [] # Thread_id: 3  Schema:   QC_hit: No # Query_time: 0.000123  Lock_time: 0.000000  Rows_sent: 1  Rows_examined: 0 # Rows_affected: 0  Bytes_sent: 49
SET timestamp=1700038768;
SET STATEMENT max_statement_time=60 FOR SELECT 1;

注意:所有以 # 字符开头的行将显示在一行中

谢谢你们 !

答案1

以下是使用 Perl 完成此作业的一种方法:

perl -0777 -ape 's/\R(?=#)/ /g' file
2023-11-15T08:59:28.000000+00:00 database-1 # Time: 231115  8:59:28 # User@Host: rdsadmin[rdsadmin] @ localhost [] # Thread_id: 3  Schema:   QC_hit: No # Query_time: 0.000123  Lock_time: 0.000000  Rows_sent: 1  Rows_examined: 0 # Rows_affected: 0  Bytes_sent: 49
SET timestamp=1700038768;
SET STATEMENT max_statement_time=60 FOR SELECT 1;

在哪里:

-0777                   # option that makes the file to be considered as a one line file
-ape                    # loop, print and execute
's/\R(?=#)/ /g'         # substitute any linebreak followed by "#" with a space
file                    # file to be processed

相关内容