linux - 在 Bash 中格式化文本输出

标签 linux bash awk sed formatting

我有一些来自 pdsh 的输出,我想将其格式化以加载到数据库中。我需要它在 3 列中,并且基本上希望模块 ID 和消息在一行中。波纹管是我的输出:

10.125.45.58,scope, Module ID     = server-1
10.125.45.58,scope, Message       = Correctable memory error
10.125.45.58,scope, Module ID     = server-2
10.125.45.58,scope, Message       = Correctable memory error 

这是我需要的输出:

10.125.45.58,scope,server-1 Correctable memory error
10.125.45.58,scope,server-2 Correctable memory error

我一直在 awking 和 seding...我无法解决任何问题?

这是我到目前为止所做的

cat myfile.txt | sed -e "s/:/\,scope\,/g" | grep -E '(Module ID|Message)'

谢谢。

最佳答案

$ cat ip.txt 
10.125.45.58,scope, Module ID     = server-1
10.125.45.58,scope, Message       = Correctable memory error
10.125.45.58,scope, Module ID     = server-2
10.125.45.58,scope, Message       = Correctable memory error 

$ sed 'N; s/\n.*=//; s/ *Module ID.*= *//' ip.txt 
10.125.45.58,scope,server-1 Correctable memory error
10.125.45.58,scope,server-2 Correctable memory error 
  • N 获取下一行,因此替换适用于同时考虑的每两行
  • s/\n.*=//; 删除从换行符到最后一个=
  • s/*Module ID.*= *// 然后删除从 Module ID= 的所有内容,周围有可选空格

关于linux - 在 Bash 中格式化文本输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40405255/

相关文章:

comments - AWK:是否有一些标志可以忽略评论?

linux - 防止 fork() 复制套接字

android - 没有 Android 的嵌入式 Linux 的 3D 图形引擎

linux - inotifywait -m 长时间运行后不会处理超过 1 个文件

linux - stat 在命令行中起作用,但在传递给变量时不起作用

bash - awk 按空格分割行,其中每个字段都有空格

sed - 将新行视为另一个字符的 grep/sed 的替代方案

android - 为什么网关是bogon?

将绝对路径转换为当前存在的相对路径的 Linux 命令

bash - 通过键盘输入暂停脚本