我的输入文件如下:
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 "%s" OFS, $0; next}
{ gsub(/(^|\n)[[:blank:]]+/,OFS) }1' file
此解决方案只会删除偶数 block 开头的间距。这与将所有间距替换为单个间距的其他解决方案相反。
关于awk命令解析由新行分隔的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60070982/