LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Help with a simple shell script (https://www.linuxquestions.org/questions/programming-9/help-with-a-simple-shell-script-896763/)

Q_Linux 08-15-2011 04:42 PM

Quote:

Originally Posted by MTK358 (Post 4440960)
Code:

grep state:unreachable | wc -l

I've been trying and getting frustrated :-( How do I add another instance of grep. For example, how do I add another one in the same line.

I have this grep -c 'state:unreachable', but I need to add 'state:invalid'. To get a count of the "invalids"

MTK358 08-15-2011 06:13 PM

The problem is that I don't know where you want the output. Anyway, maybe something like this:

Code:

awk 'BEGIN {
        unreachable_count = 0
        invalid_count = 0
    }
    /state:unreachable/ {
        unreachable_count++
    }
    /state:invalid/ {
        invalid_count++
    }
    END {
        print "Invalid Count: " invalid_count
        print "Unreachable Count: " unreachable_count
    }'


Q_Linux 08-15-2011 07:16 PM

Quote:

Originally Posted by MTK358 (Post 4443956)
The problem is that I don't know where you want the output. Anyway, maybe something like this:

Code:

awk 'BEGIN {
        unreachable_count = 0
        invalid_count = 0
    }
    /state:unreachable/ {
        unreachable_count++
    }
    /state:invalid/ {
        invalid_count++
    }
    END {
        print "Invalid Count: " invalid_count
        print "Unreachable Count: " unreachable_count
    }'


The output can display on the screen. I just need a count to appear.

Q_Linux 08-16-2011 09:09 AM

Quote:

Originally Posted by MTK358 (Post 4443956)
The problem is that I don't know where you want the output. Anyway, maybe something like this:

Code:

awk 'BEGIN {
        unreachable_count = 0
        invalid_count = 0
    }
    /state:unreachable/ {
        unreachable_count++
    }
    /state:invalid/ {
        invalid_count++
    }
    END {
        print "Invalid Count: " invalid_count
        print "Unreachable Count: " unreachable_count
    }'



I tried that, it worked fine in its form. I've been trying to code it in such a way to have both counts = a single numeric output. Meaning "invalid" + "unreachable" = 5 (as an example)

MTK358 08-16-2011 11:18 AM

How about this:

Code:

print invalid_count + unreachable_count
?

Q_Linux 08-16-2011 01:00 PM

Quote:

Originally Posted by MTK358 (Post 4444711)
How about this:

Code:

print invalid_count + unreachable_count
?

Thanks!!


All times are GMT -5. The time now is 06:54 PM.