简体   繁体   中英

AWK : parsing output of multiple commands

Below is my script that I am using to grep all errors / exceptions from my application logs:

$ 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 process output of:

application logs
vmstat
top 
sar

etc

Please note that we have awk and gawk only installed in our boxes.

Here's one way using GNU awk . Run like:

awk -f script.awk

Contents of 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 . Now the onus is on you to do something with var[1-3]. Please let me know if I can help further. Goodluck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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