Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a file that contains results grouped into categories. For example:

checking results for $var
here are some results1 for $var status ok
here are some results2 for $var status ok
here are some results3 for $var status ok

checking results for $var2
here are some results1 for $var2 status ok

checking results for $var3
here are some results1 for $var3 status ok
here are some results2 for $var3 status ok
here are some results3 for $var3 status critical
here are some results4 for $var3 status ok

I need to check if any of the lines in a given category had status "critical", and display it if so. For example:

Status $var -- OK
Status $var2 -- OK
Status $var3 -- Critical

Note : Critial at $result3 for $var3

Using grep -A would not help much because the number of results under a particular variable varies.

share|improve this question
2  
Could you post an example of your input files? It's a bit hard to understand exactly what you have and what you want. –  Mat Oct 4 '11 at 8:22
    
the example i posted is how the files are listed. at the end of the script i need to print out the $var status if its ok or critical. –  afbr1201 Oct 4 '11 at 8:31
    
Take a look at Ack betterthangrep.com –  Johnsyweb Oct 4 '11 at 9:27
    
@afbr1201: still not very clear. I understand the type of output you are looking for, but not the format of the source. What you posted looks processed already (is that correct?). If that's the case, what's the format of the original data? (Might be easier to get what you want directly from there.) –  Mat Oct 4 '11 at 10:15
    
@Mat : yeah, this is a processed output already., i don't have access from the backend (who parse this type of result)., so i can only parse this output. –  afbr1201 Oct 4 '11 at 23:39
add comment

2 Answers

up vote 2 down vote accepted

Awk will do it:

awk '
    BEGIN {
        priority["ok"] = 10
        priority["critical"] = 20
    }
    /^$/ {next}
    /^checking/ {var = $NF}
    {
        if (priority[status[var]] < priority[$NF])
            status[var] = $NF
        if ($NF == "critical")
            crit[var ":" $4] = 1
    }
    END {
        for (var in status)
            print var, status[var]
        for (c in crit) {
            split(c, ary, /:/)
            printf("critical at %s for %s\n", ary[2], ary[1])
        }
    }
'

outputs

$var ok
$var2 ok
$var3 critical
critical at results3 for $var3
share|improve this answer
add comment

I'd use an awk script:

/^checking results for / {
    if (curvar) { print curvar, curstatus; }
    curvar = $NF
    curstatus = "ok"
}
/^here are some results.* for .* status / {
    if ($NF != "ok") { curstatus = $NF; 
}
END {
    if (curvar) { print curvar, curstatus; }
}

You could use an array to build up the "bad results" table.

share|improve this answer
1  
missing a close brace on one of the if statements –  glenn jackman Oct 4 '11 at 19:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.