grep for a string until instance of a space

Hey guys,

I'm having a bit of trouble getting this to work using either sed or grep. It's possible awk might be the ticket I need as well, but my regulat expression skills aren't quite up to the task for doing this.

I'm looking to grep for the string ERROR from the following log up until any instance of a space.

Here's an example of the log.

[24/Nov/2011:14:11:15] /root/bin/dbbackup.sh: started
[24/Nov/2011:14:11:15] 12591 > /var/run/dbbackup.pid

[24/Nov/2011:14:11:15] example.com started	1/3

[24/Nov/2011:14:11:15] example2.com started	2/3
[24/Nov/2011:14:11:15] ERROR: example2.com failed
mysqldump: Couldn't execute 'show create table `bbs`': Can't open file: 'bbs.ibd' (errno: 1) (1016)
mysqldump: Couldn't execute 'show create table `bbs`': Can't open file: 'bbs.ibd' (errno: 1) (1016)

[24/Nov/2011:14:11:15] example3.com started	3/3
[24/Nov/2011:14:11:15] ERROR: example3.com failed
mysqldump: Couldn't execute 'show create table `bbs`': Can't open file: 'bbs.ibd' (errno: 1) (1016)
mysqldump: Couldn't execute 'show create table `bbs`': Can't open file: 'bbs.ibd' (errno: 1) (1016)

[24/Nov/2011:14:11:58] /root/bin/dbbackup.sh: Finished after 0min and 43sec

If the grep was successful, my output would basically look like this:

[24/Nov/2011:14:11:15] ERROR: example2.com failed
mysqldump: Couldn't execute 'show create table `bbs`': Can't open file: 'bbs.ibd' (errno: 1) (1016)
mysqldump: Couldn't execute 'show create table `bbs`': Can't open file: 'bbs.ibd' (errno: 1) (1016)
[24/Nov/2011:14:11:15] ERROR: example3.com failed
mysqldump: Couldn't execute 'show create table `bbs`': Can't open file: 'bbs.ibd' (errno: 1) (1016)
mysqldump: Couldn't execute 'show create table `bbs`': Can't open file: 'bbs.ibd' (errno: 1) (1016)

Any help on this is appreciated. thanks!:slight_smile:

awk 'F &&! NF{exit}/ERROR/{F++}F' file

You'll want to grep -n to identify the line number, and then you could use your line number as a part of a sed script...

this is too awkward...

sed -n '/ERROR/,/^$/p' myFile | sed '/^$/d'

Thanks, Looks like this works. Although I forgot to mention its possible this will occur more than once in the log file. Your solution only outputs the first instance. :stuck_out_tongue: Any idea if this can be done?

Unfortunately this isn't do-able since the errors won't always occur on the same lines in the log file. But, thanks for the insight!:slight_smile:

---------- Post updated at 04:11 PM ---------- Previous update was at 04:10 PM ----------

This did the trick! thanks!:slight_smile:

Remove "exit" and put "F=0" in danmero's code and you have it!

awk 'F &&! NF{F=0}/ERROR/{F++}F' file

--ahamed

Sweet, this works as well. Thanks for the help.