Removing certain lines from results - awk

im using the code below to monitor a file:

gawk '{
    a["accepted"] += gsub("(^| )accepted( |$)", "&")
    a["open database"] += gsub("(^| )open database( |$)", "&")
} END {
    for (i in a)
        printf("%s=%s\n", i, a)
}' /var/log/syslog

the code is searching the syslog file for the string "accepted" and "open database". and when it finds them, it gives a total count for each string.

but now, using the same exact logic of this code, i want to be able to exclude certain lines from the result.

kind of like what egrep would do:

egrep accepted /var/log/syslog | egrep -vc "coin nex_1.u"

so if i wanted to exclude all lines that contain the string "coin nex_1.u" if they are on the same lines found containing "accepted", how would i modify the above awk code to do that?

Try changing:

    a["accepted"] += gsub("(^| )accepted( |$)", "&")

to

    if(!/coin nex_1\.u/) a["accepted"] += gsub("(^| )accepted( |$)", "&")
1 Like

You don't need to use grep alongside awk.

awk '!/coin nex_1.u/ {
    a["accepted"] += gsub("(^| )accepted( |$)", "&")
}
{
    a["open database"] += gsub("(^| )open database( |$)", "&")
} END {
    for (i in a)
        printf("%s=%s\n", i, a)
}' /var/log/syslog
1 Like