Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

grepping with variables for logfiles

Status
Not open for further replies.

redsevens

IS-IT--Management
Aug 11, 2000
40
0
0
US
I'm trying to create a script that will assess how many entries per hour are in a log file (/var/log/maillog). The code I have is below:
Code:
#!/bin/bash
for monthname in "Jan" "Feb" "Mar" "Apr" ... "Dec"
do
   for daynum  in "01" "02" "03" "04" "05" ... "31"
   do
      for hournum in "00" "01" "02" "03" ... "23"
      do
         grepstring="\"$monthname $daynum $hournum:\"";
         grepresult=`grep $grepstring /var/log/maillog | wc -l;`;
         echo "$monthname $daynum $hournum:$grepresult";
      done
   done
done
The lists are abbreviated in the post, but spelled out completely in the script. The innermost command is supposed to get a line count for statements such as
Code:
grep "Jan 01 04:" /var/log/maillog | wc -l
which works when I type it in at the command prompt, but not in the script. Is there something really simple I am missing here?
 
Hi red,

Your line:

grepstring="\"$monthname $daynum $hournum:\"";

do you really need the escaped " characters? try:

grepstring="$monthname $daynum $hournum:";
Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
The quotes were needed in the grep command so I could search for a string with a space in it. I managed to fix it though, by doing the following:
Code:
grepstring="$monthname $daynum $hournum:";
...
grepresults=`grep "$grepstring" /var/log/maillog | wc -l;
I guess it all boils down to how bash substitutes values for variables. Thanks for the suggestion Mike.

 
that's what I said actually :) didn't say it clearly enough though obviously.

Glad you're sorted.

Regards
Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
A pedantic point red, but you *could* also say

grepresults=`grep -c "$grepstring" /var/log/maillog`

:)

Greg.
 
yes - I always forget about the -c option in grep Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top