grep for a string until instance of a space


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting grep for a string until instance of a space
# 1  
Old 11-27-2011
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.

Code:
[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:

Code:
[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)

Code:
[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!^_^

Last edited by terrell; 11-27-2011 at 05:04 PM..
# 2  
Old 11-27-2011
Code:
awk 'F &&! NF{exit}/ERROR/{F++}F' file

# 3  
Old 11-27-2011
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...
# 4  
Old 11-27-2011
Quote:
Originally Posted by curleb
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...
Code:
sed -n '/ERROR/,/^$/p' myFile | sed '/^$/d'

# 5  
Old 11-27-2011
Quote:
Originally Posted by danmero
Code:
awk 'F &&! NF{exit}/ERROR/{F++}F' file

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. Smilie Any idea if this can be done?

Quote:
Originally Posted by curleb
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...
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!^_^

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

Quote:
Originally Posted by vgersh99
this is too awkward...
Code:
sed -n '/ERROR/,/^$/p' myFile | sed '/^$/d'

This did the trick! thanks!^_^
# 6  
Old 11-27-2011
Remove "exit" and put "F=0" in danmero's code and you have it!
Code:
awk 'F &&! NF{F=0}/ERROR/{F++}F' file

--ahamed
# 7  
Old 11-27-2011
Quote:
Originally Posted by ahamed101
Remove "exit" and put "F=0" in danmero's code and you have it!
Code:
awk 'F &&! NF{F=0}/ERROR/{F++}F' file

--ahamed
Sweet, this works as well. Thanks for the help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Grep command in Linux in a script where the search string has a space

I have a file xyz with the following content PPPL 0123 PPPL 0006 POFT 0923 POFT 1111 WENT 2323 SEND 2345 I also have another file named MasterFile where it contains the above mentioned data million times with different digits at the end for example some times it contains SEND 9999 or WENT... (4 Replies)
Discussion started by: knijjar
4 Replies

2. Shell Programming and Scripting

One instance of comparing grep and awk

Hi. In thread https://www.unix.com/shell-programming-and-scripting/267833-grouping-counting.html rovf and I had a mini-discussion on grep and awk. Here is a demo script that compares the awk and grep approaches for this single problem: #!/usr/bin/env bash # @(#) s2 Demonstrate group... (1 Reply)
Discussion started by: drl
1 Replies

3. UNIX for Advanced & Expert Users

Grep the only instance name

Hi, I want to get the only application name from the server. Ex: if i give $ ps -ef | grep bw. It will show all BW process with entire path. It will little confuse to list out the process. Can anyone have syntax to get only the instance name. I need this for be, hawk,ems also. Please... (2 Replies)
Discussion started by: ckchelladurai
2 Replies

4. Shell Programming and Scripting

Grep line with all string in the lines and not space.

I want to write the syntax so does not count line with no space. So currerntly it is showing lines as 5, but i want to show 4. # cat /tmp/mediacheck | sort -u | grep -vi " " | awk '{print $1}' | wc -l BA7552 BAA002 BAA003 BAA004 (6 Replies)
Discussion started by: Junes
6 Replies

5. Shell Programming and Scripting

How can I just grep one instance of a word in the file

I want to grep some information out of the dmidecode but when I type dmidecode | grep Memory I get several instances of the word. Is there a way I can just choose which instance I want to display? (8 Replies)
Discussion started by: jcnewton13
8 Replies

6. Shell Programming and Scripting

grep second instance of same string

Hi all, i am new to unix scripting in ksh or any shell for that matter. I have downloaded a xml file from a website and saved on my local harddrive. inside the xml, the same tag is listed multiple times. <title>Tonight</title> <title>Thursday</title> <title>Friday</title>... (6 Replies)
Discussion started by: scubasteve39
6 Replies

7. Shell Programming and Scripting

Search text file, then grep next instance of string

I need to be able to search for a beginning line header, then use grep or something else to get the very next instance of a particular string, which will ALWAYS be in "Line5". What I have is some data that appears like this: Line1 Line2 Line3 Line4 Line5 Line6 Line7 Line1 Line2 ...... (4 Replies)
Discussion started by: Akilleez
4 Replies

8. UNIX for Dummies Questions & Answers

Matching exact string with blank space using grep

hi! i'm trying to get grep to do an exact match for the following pattern but..it's not quite working. I'm not too sure where did I get it wrong. any input is appreciated. echo "$VAR" | grep -q '^test:]name' if ; then printf "test name is not found \n" fi on... (4 Replies)
Discussion started by: jazzaddict
4 Replies

9. AIX

grep not working when search string has a space in it

Hi, I am trying to grep a string which has two words separated by space. I used a script to grep the string by reading the string in to a variable command i used in the script is echo "enter your string" read str grep $str <file> it is working fine when the entered string is a single... (3 Replies)
Discussion started by: sekhar gajjala
3 Replies

10. Shell Programming and Scripting

find the first instance after a string

I have this file (below) and need to get out specific data that appears after OSE1_1.FIX, but before OSE1_2.FIX. Specifically I need to get all of the data after "ROW80_20:", "ROW80_21:", "ROW80_22:" & "ROW80_23:" then I need to do the same for data the appears after OSE1_2.FIX, but before... (2 Replies)
Discussion started by: josslate
2 Replies
Login or Register to Ask a Question