bash - shell 脚本: Use command in awk

标签 bash shell awk manpage

这是我的代码

awk '{a[$1":"$5]}
    END{for(i in a)
            {
            split(i,b,":");
            split(b[2],c,"[");
    print b[1],b[2]
            }
       }' /var/log/messages

输出为:(显示月份和进程名称)

May init
May rhsmd
May kernal

我想将进程名称更改为简短描述。 简短描述基于“man”文档。

这个命令帮助我打印我想要的内容。

man init | sed -n '6p' | cut -c 8-

输出:

init - Upstart process management daemon

最后,我找不到将“man”代码嵌入到 awk 的方法。 以下是我预期的最终输出, 我怎样才能做到这一点?谢谢。

May init - Upstart process management daemon
May rhsmd - A Program for querying the Red Hat Network for updates and information
May kernal

有一些/var/log/messages 的示例

May 21 03:30:02 redhat rhsmd: This system is registered to RHN Classic.
Sep 22 03:35:02 redhat rhsmd: This system is registered to RHN Classic.
May 22 13:00:31 redhat init: serial (hvc0) main process (1326) killed by TERM signal
May 22 13:00:31 redhat init: tty (/dev/tty6) main process (1336) killed by TERM signal
May 22 13:00:32 redhat rhnsd[1256]: Exiting

最佳答案

我会为此使用 shell。

awk '{a[$1":"$5]}
    END{for(i in a)
        {
        split(i,b,":");
        split(b[2],c,"[");
print b[1],b[2]
        }
   }' /var/log/messages |
while read month cmd; do
    echo -n "$month "
    whatis "$cmd"
done

关于bash - shell 脚本: Use command in awk,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23846423/

相关文章:

bash:使用精美的 PS1 进行换行,进行位置回显

bash - reg for . 中的双反斜杠和单斜杠有什么区别?

perl - 用于从文件中提取特定行的最快/最简洁的 bash 单行代码

jquery - 如何使用 bash 或 perl 解析 jquery

linux - 在 Windows 上的 bash 上运行 Kestrel

bash - 有没有办法让 bash 作业控制安静?

shell - 从命令行更改文件夹权限(在 mac 上)

shell脚本引用

linux - 如何过滤某个字段中有尾随空格的行?

linux - 停止 shell 脚本发送重复的电子邮件 - 使用文件标志