awk命令来分析由新行分隔的文件

问题描述 投票:0回答:4

我的输入文件如下:

OS ABI

   UNIX - System V

CPU Class

   64-bit

Persistence (user)

   No

File type 

   ELF 32-bit MSB executable, PowerPC or cisco 4500, version 
   (SYSV),statically linked, not   stripped,32-bit MSB executable, 
   PowerPC or cisco 4500, version (SYSV),statically linked, not stripped

CPU Type

  PowerPC

我希望输出为:

OS ABI UNIX - System V
CPU CLASS 64-bit
Persistence (user) No
File Type ELF 32-bit MSB executable, PowerPC or cisco 4500, version (SYSV),statically linked, not   stripped,32-bit MSB executable, PowerPC or cisco 4500, version (SYSV),statically linked, not stripped 
CPU Type PowerPC

请使用awk建议脚本/命令,该脚本/命令可以输出所需的结果?当我们必须解析字段5中所示的多行时,问题就来了。我也希望对每个字段中的行数进行计数,例如:字段1有一行,字段5有2等。

awk newline
4个回答
2
投票

这将为您完成:

awk 'BEGIN{RS=""}
     !/^[[:blank:]]/{printf $0 OFS; next}
     { gsub(/(^|\n)[[:blank:]]+/,OFS) }1' file

1
投票

EDIT:


1
投票

另一个awk变体:


0
投票
$ awk -v RS= '{gsub(/[[:space:]]+/," "); printf "%s%s", $0, (NR%2?"":ORS)}' file
OS ABI UNIX - System V
CPU Class 64-bit
Persistence (user) No
File type  ELF 32-bit MSB executable, PowerPC or cisco 4500, version (SYSV),statically linked, not stripped,32-bit MSB executable, PowerPC or cisco 4500, version (SYSV),statically linked, not stripped
CPU Type PowerPC
© www.soinside.com 2019 - 2024. All rights reserved.