简体   繁体   English

AWK:解析多个命令的输出

[英]AWK : parsing output of multiple commands

Below is my script that I am using to grep all errors / exceptions from my application logs: 以下是我用来从应用程序日志中grep所有错误/异常的脚本:

$ tail -f -n 0 /web/*/logs/*.log | awk '{ if ( $0 ~ /==>.*<==/) { print "File :" $0 } else if( $0 ~ /error/ || $0 ~ /Error/ || $0 ~ /exception/ || $0 ~ /Excpetion/ || $0 ~ /ORA-/ || $0 ~ /fatal/ || $0 ~ /Fatal/){print "Error " $0}}'

The output comes out fine but my requirement is that I need to parse output of other system commands parallely like: 输出结果很好,但是我的要求是我需要并行解析其他系统命令的输出:

vmstat
top 
sar 

etc. 等等

Is there a way that via running a single awk command I can get output of all the command something like: 有没有一种方法可以通过运行单个awk命令来获取所有命令的输出,例如:

awk process output of:

application logs
vmstat
top 
sar

etc 等等

Please note that we have awk and gawk only installed in our boxes. 请注意,我们仅在盒子中安装了awk和gawk。

Here's one way using GNU awk . 这是使用GNU awk的一种方法。 Run like: 运行像:

awk -f script.awk

Contents of awk.script : awk.script内容:

BEGIN {
    printf "Process output of:\n"

    command0="tail -f -n 0 /web/*/logs/*.log"
    command1="vmstat"
    command2="free"
    command3="ps -eo pcpu,pid,user,tty,args"

    printf "\n>%s:\n\n", command0
    while ( (command0 |& getline var0) > 0) {
        if (var0 ~ /==>.*<==/) {
            print "File :" var0
        }
        else if (var0 ~ /[Ee]rror|[Ee]xception|ORA|[Ff]atal/) {
            print "Error", var0
        }
    }

    printf "\n>%s:\n\n", command1
    while ( (command1 |& getline var1) > 0) {
        print var1
    }

    printf "\n>%s:\n\n", command2
    while ( (command2 |& getline var2) > 0) {
        print var2
    }

    printf "\n>%s:\n\n", command3
    while ( (command3 |& getline var3) > 0) {
        print var3
    }
}

As you can see, this script will run four commands, tail , vmstat , free and ps . 如您所见,此脚本将运行四个命令: tailvmstatfreeps Now the onus is on you to do something with var[1-3]. 现在,您有责任对var [1-3]进行操作。 Please let me know if I can help further. 如果可以进一步帮助,请告诉我。 Goodluck. 祝好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM