1

So I'm trying to create a bash script that looks for two words in my syslog file. Then I want the script to print out how many times those two words have appeared. Also I want it to print it out for every hour of the day. So like if the word dog appeared 4 times during the first hour of today, it says Hour one, dog 4. Finally at the end of the script I want it to print out how many times those words appeared all day.

The sudo code I have thus far is

if 2 > hour
find permit
find block
print both
finish

if 1 < hour < 2
find permit
find block
print both
finish

if 2 < hour < 3
find permit
find block
print both
finish

command is grep -o "\WORD\" Syslog.txt * | sort | uniq -c

1

Here is some less-pseudo code. I have not tested it so it may not work at all, but it's where I might start.

function findprint {
    local word1 = $(echo $1 | grep -o "\WORD1\" | wc -w)
    local word2 = $(echo $1 | grep -o "\WORD2\" | wc -w)
    word1total=$[word1total+word1]
    word2total=$[word2total+word2]
    echo "$word1 WORD1"
    echo "$word2 WORD2"
}
word1total = 0;
word2total = 0;
while read line; do
    if [ hour -lt 2 ]; then
        echo "Hour 1"
        findprint "$line"
    fi

    if [ hour -gt 1 && hour -lt 2 ]; then
        echo "Hour 2"
        findprint "$line"
    fi

    if [ hour -gt 2 && hour -lt 3 ]; then
        echo "Hour 3"
        findprint "$line"
    fi
done < /var/log/syslog

echo "TOTAL:"
echo "$word1total WORD1"
echo "$word2total WORD2"
  • Getting a syntax error at the second to last line. "Unexpected EOF while looking for matching `'" Will continue to figure it out of course. – M. Wolf Jun 6 '14 at 18:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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