我的输入文件如下:
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 'BEGIN{RS=""}
!/^[[:blank:]]/{printf $0 OFS; next}
{ gsub(/(^|\n)[[:blank:]]+/,OFS) }1' file
EDIT:
另一个awk
变体:
$ 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