LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 03-31-2011, 03:49 AM   #1
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Rep: Reputation: 12
filtering with grep?


Hi all

small and (quick) question

In a logfile i get errors like ERROR_blabla or blabla_ERROR.
How can i test on only ERROR?
if i use:
Code:
GREPPED=`grep -ie "error" /some/folder/logfile.txt`
it doesn't work, which is logical, but i've found something (wildcards),
so i tried
Code:
GREPPED=`grep -ie ".error." /some/folder/logfile.txt`
a '.' is one character (assuming the website is right), but it still doesn't work...

So how can i search for the error with a wildcard?

thx
 
Old 03-31-2011, 03:54 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
What do you mean for "it still doesn't work"? Can you show what do you get and what is the desired result? Indeed, in a regular expression the dot stats for any single character, so that the expression
Code:
.error.
should match every line containing error preceded by at least one character and followed by at least one character (spaces included - it doesn't match the word error if it is at the beginning or at the end of the line). Please show us a real line of input and the wanted output. Thanks.
 
Old 03-31-2011, 03:58 AM   #3
Sjonnie48
Member
 
Registered: Jun 2005
Location: Earth
Distribution: Ubuntu10.04
Posts: 308

Rep: Reputation: 43
To use grep, see example:

Code:
#cat kern.log | grep RAM
Mar 27 12:21:04 server kernel: [    0.000000] BIOS-provided physical RAM map:
Mar 27 12:21:04 server kernel: [    0.000000] modified physical RAM map:
Mar 27 12:21:04 server kernel: [    0.000000] RAMDISK: 37827000 - 37feff95
The | symbol is a pipe thru which the output of cat is transferred to grep
 
Old 03-31-2011, 04:00 AM   #4
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Original Poster
Rep: Reputation: 12
this is working:
Code:
GREPPED=`grep -ie "error[_]" /home/vusbe/logfile.txt`
maybe the '.' works, but i've noticed that i forgot something, maybe you can help

i'm testing what i grepped with an IF , like:
Code:
if [ "$GREPPED" = "error" ] || [ "$GREPPED" = "ERROR" ]; then
but i don't grep an error anymore, but an error_ so ofcourse the if doesn't do his job, can you tell me what wildcard i need to use to get it to work?

thx
 
Old 03-31-2011, 04:09 AM   #5
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by brownie_cookie View Post
this is working:
but i don't grep an error anymore, but an error_ so ofcourse the if doesn't do his job, can you tell me what wildcard i need to use to get it to work?
It's still not clear what you're trying to achieve, so that we cannot explain why the expression with dots doesn't work. Regarding your last question, if using bash version 3 or newer, you can try the =~ operator to match against a regular expression. Or even better test the grep command directly:
Code:
if grep -ie error /home/vusbe/logfile.txt
then
  :
fi
 
Old 03-31-2011, 04:11 AM   #6
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Original Poster
Rep: Reputation: 12
in a logfile i can have either ERROR, ERROR_ or _ERROR
so i need to grep it with a wildcard
i geuss it's like
Code:
grep=`grep -ie ".error." /some/folder/log.txt`
but that doesn't work, because when i run it, i echo that result and i get nothing...

now, but let's assume it's working, what about the IF ?
so i need to do something if i get a result like ERROR, ERROR_ or _ERROR

so i geuss something like:
Code:
if [ "$grep" = "*error*" ] || [ "$grep" = "*ERROR*" ]; then
blabla
fi
am i right??

Last edited by brownie_cookie; 03-31-2011 at 04:12 AM.
 
Old 03-31-2011, 04:33 AM   #7
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by brownie_cookie View Post
but that doesn't work, because when i run it, i echo that result and i get nothing...
Sorry, but still we don't believe it. Again, better if you show us some real lines, the command you've tried, the exact output. Example of what I mean:
Code:
$ cat file
something ERROR_or something else
just a WARNING here
whatever ERROR is it
maybe get_ERROR for nothing
$ grep -i error file
something ERROR_or something else
whatever ERROR is it
maybe get_ERROR for nothing
$ grep -io error file
ERROR
ERROR
ERROR
Quote:
Originally Posted by brownie_cookie View Post
so i need to do something if i get a result like ERROR, ERROR_ or _ERROR
As mentioned above, you can test directly the grep command. Every command in linux gives an EXIT STATUS, which usually is 0 for success, 1 or any other number for failure. You can retrieve it using $? immediately after the command. Example:
Code:
$ grep -i error file
something ERROR_or something else
whatever ERROR is it
maybe get_ERROR for nothing
$ echo $?
0
$ grep -i goofy file
$ echo $?
1
that is success and failure respectively. If you put the command in an if/then statement the exit status is evaluated and it results in a boolean expression: 0 is true, any other number is false. Example:
Code:
$ if grep -iq error file; then echo error found; fi
error found
$ if grep -iq goofy file; then echo goofy found; fi
$
Hope this helps.
 
Old 03-31-2011, 04:41 AM   #8
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Original Poster
Rep: Reputation: 12
i have found the problem on a website:
Quote:
By default, grep doesn’t search for whole words. Instead it searches for text strings.
i've forgot that
so my code is now:
Code:
GREPPED=`grep -ie "warning" -ie "error" /some/folder/logfile.txt`
IF statement:
Code:
if [ "$GREPPED" = "error" ] || [ "$GREPPED" = "ERROR" ] || [ "$GREPPED" = "_error" ] || [ "$GREPPED" = "ERROR_" ] || [ "$GREPPED" = "_error" ] || [ "$GREPPED" = "_ERROR" ]; then
and it's working the other codes i'm using are rather irrelevant, because those are just numbers i've a get an error i put a number in a variable

but it's working
maybe you have an idea how i can shorten the IF statement?

thx
 
Old 03-31-2011, 05:19 AM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
Quote:
Originally Posted by brownie_cookie View Post
maybe you have an idea how i can shorten the IF statement?
By means of
Code:
shopt -s nocasematch
and the =~ operator, but since you seem to ignore previous suggestions in favor of your monologue, it's pretty not useful to spend more words. Good luck!
 
Old 03-31-2011, 05:25 AM   #10
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Original Poster
Rep: Reputation: 12
[mod_edit]rudeness removed[/mod_edit]

Last edited by Tinkster; 03-31-2011 at 12:35 PM.
 
Old 03-31-2011, 05:31 AM   #11
markush
Senior Member
 
Registered: Apr 2007
Location: Germany
Distribution: Slackware
Posts: 3,979

Rep: Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850Reputation: 850
hello brownie cookie,

your if statement seems like nonsense to me. Did you read the manpage for grep? Are you sure that you've understood your own question before you started this thread?

Well, I agree with colucix, but since I tried it out by myself and found a solution, here it is
Code:
grep -i "_\?error_\?" yourfile
the questionmark matches if the preceding character is there or not.

Markus
 
Old 03-31-2011, 05:40 AM   #12
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Original Poster
Rep: Reputation: 12
nah, it doesn't work... i copied your possible solution and pasted it in my code and changed it to my needs
and noooo it doesn't work

Code:
GREPPED=`grep -ie "_\?error_\?" /some/folder/logfile.txt`

fail
thx anyways

P.S.: this one is solved thx for your replies

Last edited by brownie_cookie; 03-31-2011 at 05:41 AM.
 
Old 03-31-2011, 05:47 AM   #13
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Assuming that all of your correct replies are distinct strings separated by white space or newlines, this could work:
Code:
if egrep -i '\b_?error_?\b' file
 
Old 03-31-2011, 05:54 AM   #14
brownie_cookie
Member
 
Registered: Mar 2011
Location: Belgium
Distribution: CentOS release 5.5 (Final), Red Hat Enterprise Linux ES release 4 (Nahant Update 8)
Posts: 416
Blog Entries: 2

Original Poster
Rep: Reputation: 12
that ain't working either

but guys, i give a big THANK YOU to ALL of you who tried to help me (even if you were agitated ) but i got my solution, it is working:

Code:
GREPPED=`grep -ie "warning" -ie "error" /home/vusbe/logfile.txt`
this is ALL i needed, i had it already but i made a mistake in my IF statement...

so no need to reply again

and i quote from Grail:
Quote:
Happy with solution ... mark as SOLVED
THANKS AGAIN folks
 
Old 03-31-2011, 07:12 AM   #15
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
Well remembering it is a 2 way street I ma guessing those like myself would want to know why our solutions do not work?
My guess would the data you are searching is not as you defined in previous parts of the question.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Filtering out duplicate lines from a find/grep output thundervolt Linux - Newbie 10 03-25-2010 04:32 AM
Trying to understand pipes - Can't pipe output from tail -f to grep then grep again lostjohnny Linux - Newbie 15 03-12-2009 11:31 PM
Content Filtering - using grep hiteshthappa Linux - Newbie 2 07-15-2008 05:11 PM
Filtering Problem (using grep) gauge73 Linux - Newbie 4 12-16-2003 11:35 PM
Sendmail Spam filtering and Virus filtering MrJoshua Linux - General 2 04-03-2003 11:12 AM


All times are GMT -5. The time now is 09:01 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration