使用awk将换行文本换成一行

时间:2019-04-29 06:33:52

标签: awk

我有一个包含以下记录的文件:

1  Error    19-03-23   02:02:26     LPU 6 : RX_PWR_L_ALARM of SFPF ALARM of                        
                                    PIC1 is abnormal[OID:1.3.6.1.4.1.201
                                    1.5.25.129.2.1.9,BasCode:67697]
2  Error    19-03-20   07:50:40     The air filter : Maybe it is not clean
                                    ed as scheduled. Please clean it and            
                                    run the reset dustproof run-time comman
                                    d[OID:1.3.6.1.4.1.2011.5.25.129.2.1.9,
                                    BasCode:67995]

我要输出:

1 Error 19-03-23 02:02:26 LPU 6 : RX_PWR_L_ALARM of SFPF ALARM of PIC1 is abnormal[OID:1.3.6.1.4.1.2011.5.25.129.2.1.9,BasCode:67697]

2 Error 19-03-20 07:50:40 The air filter : Maybe it is not cleaned as scheduled. Please clean it and run the reset dustproof run-time command[OID:1.3.6.1.4.1.2011.5.25.129.2.1.9,BasCode:67995]

2 个答案:

答案 0 :(得分:1)

GNU sed

 sed -En '/^[[:digit:]]+[[:blank:]]+/{:l1;N;/]$/!{b l1};s/\n +//g;s/ +/ /g;s/ /\t/g;s/\t/ /5gp}' file

输出

1   Error   19-03-23    02:02:26    LPU 6 : RX_PWR_L_ALARM of SFPF ALARM of PIC1 is abnormal[OID:1.3.6.1.4.1.2011.5.25.129.2.1.9,BasCode:67697]
2   Error   19-03-20    07:50:40    The air filter : Maybe it is not cleaned as scheduled. Please clean it and run the reset dustproof run-time command[OID:1.3.6.1.4.1.2011.5.25.129.2.1.9,BasCode:67995]

答案 1 :(得分:0)

请您尝试以下。

awk '
{
  if($0 ~ / +$/){
    sub(/ +$/," ")
  }
}
FNR==1{
  printf("%s",$0)
  next
}
{
  if($0 ~ /^ +/){
    check=1
    sub(/^ +/,"")
  }
  printf("%s%s",check==1?"":ORS,$0)
  check=""
}'  Input_file

以下是我得到的输出。

1  Error    19-03-23   02:02:26     LPU 6 : RX_PWR_L_ALARM of SFPF ALARM of PIC1 is abnormal[OID:1.3.6.1.4.1.2011.5.25.129.2.1.9,BasCode:67697]
2  Error    19-03-20   07:50:40     The air filter : Maybe it is not cleaned as scheduled. Please clean it and run the reset dustproof run-time command[OID:1.3.6.1.4.1.2011.5.25.129.2.1.9,BasCode:67995]