0

I need to grep the MySQL-error log starting at today's date formatted like:

`date +"%y%m%d"` (140710) 

And look for all instances of the word partitioned.

I've tried various things to no avail. This seemed to work:

more +/"`date +"%y%m%d"`" mysql-error.log | grep -c partitioned

However, if the log file doesn't contain any entries from today, then instead of returning zero, it searches the entire file and returns a number for the entire file.

I need it to return zero, if there are no entries from today, not grep the entire file and count the results.

Is there some better way of doing this, using sed or awk or something?


Technically, what I suppose I need to count is } partitioned { followed by a string of alpha numberic characters, }) because portioned { (without any string) } could occur and shouldn't be counted.

That's not a strict requirement though. I'd be happy just counting instances of the word partitioned.

Here is an example of the log file:

140109 12:25:07 [Note] WSREP: view(view_id(PRIM,c4819ddf-7952-11e3-89fc-4f9f1bfa267f,3) memb {
    c4819ddf-7952-11e3-89fc-4f9f1bfa267f,
} joined {
} left {
} partitioned {
    f85c2e70-7952-11e3-a759-aa8332246e85,
})
140109 12:25:07 [Note] WSREP: Shifting JOINED -> SYNCED (TO: 85)
  • I think you'd need something like grep -ce "$(date +%y%m%d)" -e "partitioned" /var/log/mysqld.log or whatever path your mysql log is. – Valentin Bajrami Jul 10 '14 at 11:56
1

With GNU sed anhd grep, you can try:

sed -n "/$(date +%y%m%d)/,\$p" file | grep -c partitioned

/pattern/,$ matched first pattern to the end of file.

With your input:

$ sed -n "/$(date +%y%m%d)/,\$p" 1.txt | grep -c partitioned
1

Without matching date:

$ sed -n "/$(date +%y%m%d)/,\$p" 1.txt | grep -c partitioned
0
  • Perfect - that seems to work! Thank you so much!! :) – Brad Jul 10 '14 at 15:40
1

You could try chaining two greps together:

grep `date +"%y%m%d"` mysql-error.log | grep -c partitioned

Or even simpler:

grep -c "`date +%y%m%d`.*partitioned" mysql-error.log

EDIT

As the date and the word 'partitioned' are on different lines, then:

d=$(date +"%y%m%d"); awk '/'$d'/,EOF {print $0}' mysql-error.log | grep -c partitioned

Or:

awk '/'$(date +%y%m%d)'/,EOF {print $0}' mysql-error.log | grep -c partitioned
  • So the word partitioned doesn't appear on the same line as the date. So grepping for only the date and then trying to search that doesn't work. – Brad Jul 10 '14 at 12:30
  • Confirmed the awk also works. – Brad Jul 10 '14 at 15:41

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.