连接填充了不同字段的 2 行
Concatenate 2 lines with different fields filled
我正在格式化一个日志文件,我需要将其转换为 CSV,以便我可以为上级编写报告。
在日志文件中,我得到如下内容:
Apr 15 15:51:41 server l2tps: [l2tp_l-22] RADIUS: Accounting user 'USER' (Type: 1)
Apr 15 15:51:35 server l2tps: [l2tp_l-22] RADIUS: Accounting user 'USER' (Type: 2)
在我正在处理的 CSV 中,我需要这样的东西:
User | Date | Conected on | Disconected On | Connection Time
User | 15-Apr | 15:51:35 | 15:51:41 | 00:00:06
我已经到了这样的地步,我有 2 行填充了连接或断开连接时间,并用制表符分隔字段。我已经按时间对线路进行了排序,所以在长长的列表中,我的顺序是 Conection>Disconection>Conection>Disconection... 等等。
我不知道如何连接 2 个连续的行而不覆盖填充的字段。本质上我有两行是这样的:
USER [TAB] 15-Apr [TAB] 15:51:35 [TAB] [TAB]
USER [TAB] 15-Apr [TAB] [TAB] 15:51:41 [TAB]
我需要把它变成:
USER [TAB] 15-Apr [TAB] 15:51:35 [TAB] 15:51:41 [TAB]
我这辈子都弄不明白。
TLDR:我有 2 行字段分隔数据,我需要将这两行合并为一个有空白字段的行。我该怎么做?
有人可以帮助我吗?
awk '
BEGIN{FS=OFS="1"} #Sets field separators to tab
j{x=;[=10=]=j;=x;j=0} #If j!=0, set line=j (the line before) and put the 4th field of this line in it
~/^[ ]*$/{j=[=10=]} #If 4th field is empty, j=thisline
!j #If j=0, print the line
' input
即使有注释,如果您以前从未尝试过 awk,上面的代码片段也可能难以理解。随时要求澄清。
在此输入文件上:
$ cat -T input
USER ^I 15-Apr ^I 15:51:35 ^I ^I
USER ^I 15-Apr ^I ^I 15:51:41 ^I
产生的输出是
$ cat -T <(./script)
USER ^I 15-Apr ^I 15:51:35 ^I 15:51:41 ^I
我正在格式化一个日志文件,我需要将其转换为 CSV,以便我可以为上级编写报告。 在日志文件中,我得到如下内容:
Apr 15 15:51:41 server l2tps: [l2tp_l-22] RADIUS: Accounting user 'USER' (Type: 1)
Apr 15 15:51:35 server l2tps: [l2tp_l-22] RADIUS: Accounting user 'USER' (Type: 2)
在我正在处理的 CSV 中,我需要这样的东西:
User | Date | Conected on | Disconected On | Connection Time
User | 15-Apr | 15:51:35 | 15:51:41 | 00:00:06
我已经到了这样的地步,我有 2 行填充了连接或断开连接时间,并用制表符分隔字段。我已经按时间对线路进行了排序,所以在长长的列表中,我的顺序是 Conection>Disconection>Conection>Disconection... 等等。
我不知道如何连接 2 个连续的行而不覆盖填充的字段。本质上我有两行是这样的:
USER [TAB] 15-Apr [TAB] 15:51:35 [TAB] [TAB]
USER [TAB] 15-Apr [TAB] [TAB] 15:51:41 [TAB]
我需要把它变成:
USER [TAB] 15-Apr [TAB] 15:51:35 [TAB] 15:51:41 [TAB]
我这辈子都弄不明白。
TLDR:我有 2 行字段分隔数据,我需要将这两行合并为一个有空白字段的行。我该怎么做?
有人可以帮助我吗?
awk '
BEGIN{FS=OFS="1"} #Sets field separators to tab
j{x=;[=10=]=j;=x;j=0} #If j!=0, set line=j (the line before) and put the 4th field of this line in it
~/^[ ]*$/{j=[=10=]} #If 4th field is empty, j=thisline
!j #If j=0, print the line
' input
即使有注释,如果您以前从未尝试过 awk,上面的代码片段也可能难以理解。随时要求澄清。
在此输入文件上:
$ cat -T input
USER ^I 15-Apr ^I 15:51:35 ^I ^I
USER ^I 15-Apr ^I ^I 15:51:41 ^I
产生的输出是
$ cat -T <(./script)
USER ^I 15-Apr ^I 15:51:35 ^I 15:51:41 ^I