使用awk将换行文本设为一行

使用awk将换行文本设为一行,awk,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:67

我有一个档案,有以下记录:

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]

你能试试下面的吗

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]
GNU
sed

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


欢迎来到SO!请提供一个。欢迎使用堆栈溢出!对不起,这不是StackOverflow的工作方式。形式为“我想做X,请给我提示和/或示例代码”的问题被认为是离题的。请访问并阅读,尤其是read不会在文件末尾打印换行符(
\n
)。但我提到的第一个问题仍然存在。您的终端有问题吗?@oguzismail,现在应该很好,不确定是否有新的线路,它显示ok,我会检查并更新。如果我使用此代码,我无法插入mysql。@oguz ismail:谢谢您的帮助。事实上,对于它使用的任何其他
sed
版本
GNU
扩展,这是不可能的。
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]