Simple ways to pass variations to grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simple ways to pass variations to grep
# 1  
Old 06-05-2012
Simple ways to pass variations to grep

can someone please show me a better way (if there is one), to grep out all variations (upper and small case letters) from a file?

Code:
egrep "(panic|error|failed|fail|FAIL)" /var/log/messages

I'm interested in finding any string that may indicate somethings wrong with ANY part of a system. so, if you know of any other strings outside of the ones specified above, please please please feel free to add them to the code you provide.

my systems run linux and sunos oses.

thanks
# 2  
Old 06-05-2012
What's WRONG for one may not be WRONG for another. Anyways, you place the WRONG keywords in a file (say, patts) and then do a case-insensitive grep using this pattern file...

Contents of patts could be :

Code:
FAIL
PANIC
ERROR

Then, you could use

Code:
grep -if patts <filename>

# 3  
Old 06-05-2012
Or similar to what you had already:
Code:
egrep -i "panic|error|fail" /var/log/messages

Like in elixir sinari's example, the -i switch is for "ignore case".
# 4  
Old 06-05-2012
Quote:
Originally Posted by zaxxon
Or similar to what you had already:
Code:
egrep -i "panic|error|fail" /var/log/messages

Like in elixir sinari's example, the -i switch is for "ignore case".

i was looking for something more complex. the "-i" will be slower when grepping through large files, which is what i'm dealing with.

i was thinking something along the lines of:

Code:
egrep "([pP]anic|[Ee]rror|[Ff]ail)" /var/log/messages

in this example, i know which letters may be small/upper case. so i took that into consideration. there are cases when u wont know, and i wanted to find a way to account for that, without using the "-i".

Last edited by Scrutinizer; 06-05-2012 at 10:39 AM.. Reason: code tags
# 5  
Old 06-05-2012
At any rate leave out the grouping operators ( ... ) , they seem to really slow things down...
This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 06-05-2012
Quote:
Originally Posted by Scrutinizer
At any rate leave out the grouping operators ( ... ) , they seem to really slow things down...
oh really? i wasn't aware of that all!
# 7  
Old 06-05-2012
It probably depends on implementation, so you would need to find out if they matter in your case, but FWIW, a couple of times I found these to be the culprit. Perhaps because grep not only needs to create groups, but a back reference as well. In any case you do not need them here...

Last edited by Scrutinizer; 06-05-2012 at 11:05 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove matching pattern on each line with number variations

Hello folks! I have a file containing lines like this Something text 18:37Remove This: 1,111"Keep this text" Some more text 19:37Remove This: 222"Keep this text" More text 20:50Remove This: 3,333Keep this text And more text 25:50Remove This: 44,444Keep this text I would like to... (4 Replies)
Discussion started by: martinsmith
4 Replies

2. Shell Programming and Scripting

sed Command new Line not working Tried many variations

Hi I have assigned an output of a command to $I. I try to print the input and put a new line after occurrence of the hostname which is assigned to $HOST1 ( Example: pwrm16 ) . First of all I need to get rid of the Colon after the host name pwrm16: and make it pwrm16 then I want to print the... (10 Replies)
Discussion started by: mnassiri
10 Replies

3. What is on Your Mind?

Variations on a theme...

Well you lot seriously amaze me. There was a thread a few hours ago, (on the date stamp shown in this upload), that generated loads of solutions to a fairly simple problem:- https://www.unix.com/shell-programming-scripting/235829-how-print-particular-character-n-number-times-line.html As a... (0 Replies)
Discussion started by: wisecracker
0 Replies

4. UNIX for Dummies Questions & Answers

Pass grep's Output to mv?

Hi! I was looking for files to clean out, and I remembered reading somewhere that you could pipe the output of grep to rm. But I prefer to mv stuff to ~/.Trash/ I thought I'd try something like this: ls ~/Library/Preferences/ |grep '.*.\.iTunes\.plist\..*' |xargs -J % mv % ~/.Trash/ I... (12 Replies)
Discussion started by: sudon't
12 Replies

5. UNIX for Dummies Questions & Answers

Printing all 'case' variations of a word

Hey Guys & Gals, I am trying to figure out how one would go about printing all possible variations of letter cases in a given word. So if for instance given the input "test123" output would be ; test123 Test123 TEst123 TESt123 TEST123 tEst123 tESt123 tEST123 teSt123 teST123... (1 Reply)
Discussion started by: TAPE
1 Replies

6. Shell Programming and Scripting

script to include filename with variations at the beginning of the file

Hi there, I have a bunch of files that I want to modify. As a beginner, but nevertheless enthusiast, in the use of the shell I want to create a script enabling me to modify those files quickly. I had only some partial success with various peaces of scripts but I would like to create one script... (1 Reply)
Discussion started by: iamzesh
1 Replies

7. UNIX for Dummies Questions & Answers

Simple grep question

This should be so easy... I want to find all the apps in /Applications that start with the lower case i (e.g. iTunes.app, iSync.app, iCal.app) They should all have the .app extension. I've tried: ls /Applications |grep -o i*.app ls /Applications/i*.app Anyhow, I just want to see what apps... (2 Replies)
Discussion started by: glev2005
2 Replies

8. Shell Programming and Scripting

Simple grep Question

I tried searching for answers but didn't find any. When I grep a file results read 4.2.2.2 4.4.4.2 4.5.6.7 But I just want to select each result individually. For Example I want to be able to say variable1="first grep result" variable2="second grep result" variable3="third grep... (8 Replies)
Discussion started by: elbombillo
8 Replies

9. UNIX for Dummies Questions & Answers

Grep or other ways to output line above and/or below searched line

Hi all, Would like to know how I could search for a string 'xyz' but have the output show the line plus the line above and/or below all lines found. eg. search for xyz from file containing: abc 12345 asdf xyz asdfds wwwww kjkjkj ppppp kkkxyz eeee zzzzz and the output to... (2 Replies)
Discussion started by: sammac
2 Replies

10. Shell Programming and Scripting

how to pass variable to grep?

Hi I have a such conditional: SPAMH="it is SPAM" if grep -q $SPAMH $NMDIR/$mail; then SPAMHFLAG=1 else SPAMHFLAG=0 fi And grep doesn't catch this string, even it exists there. I think it's a problem with passing $SPAMH to grep. I tried... (2 Replies)
Discussion started by: xist
2 Replies
Login or Register to Ask a Question