Searching for multiple patters using grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching for multiple patters using grep
# 1  
Old 05-08-2013
Searching for multiple patters using grep

i have a file as below

HTML Code:
grepfile.txt
----------------
RNTO command successful
No such file or directory
Authentication failed
if i seach individually for 'RNTO command successful' or 'No such file or directory' using grep -i as below, im gettting result.
Code:
 
grep -i 'No such file or directory' grepfile

Can someone tell me how to search for 'RNTO command successful' and 'No such file or directory' at a time.
# 2  
Old 05-08-2013
You want to search lines which have both those pattern or lines which have one of those?
If you have many patterns to search then put them in file and use -f option of grep command.
Man page will give you more information on same.
# 3  
Old 05-08-2013
I am not so sure.. But try following command if it works..

Code:
egrep -i '(RNTO command successful|No such file or directory)'  grepfile.txt

# 4  
Old 05-08-2013
Nakul sh's solution would work.

You can also write it like this:
egrep -i '(^R|^N)' grepfile.txt
# 5  
Old 05-08-2013
Quote:
Originally Posted by juzz4fun
Nakul sh's solution would work.

You can also write it like this:
egrep -i '(^R|^N)' grepfile.txt
Only if you assume that only those three types of lines appear in the file. I doubt that is the case. It is probably a simplified example.


To the OP:

In addition to vidyadhar85's advice, I would suggest using -F if your list is composed of literal strings and not regular expressions. Further, if they are intended to match an entire line, use -x to preclude a substring match.

Regards,
Alister

Last edited by alister; 05-08-2013 at 12:39 PM..
# 6  
Old 05-08-2013
Using awk
Code:
awk '/RNTO command successful|No such file or directory/'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. BSD

Searching in multiple files

I am new to unix and I would like to search multiple log files to find earliest occurrence of a text. Ex: Say I have 10 logs file each ending with .log and I want to find the text “CustomeError” . I want to find the which log file “CustomeError” comes first and lines which surround’s ... (4 Replies)
Discussion started by: jim john
4 Replies

2. Shell Programming and Scripting

Grep from multiple patterns multiple file multiple output

Hi, I want to grep multiple patterns from multiple files and save to multiple outputs. As of now its outputting all to the same file when I use this command. Input : 108 files to check for 390 patterns to check for. output I need to 108 files with the searched patterns. Xargs -I {} grep... (3 Replies)
Discussion started by: Diya123
3 Replies

3. UNIX for Dummies Questions & Answers

Grep - Searching for multiple items using one command

I am performing a regular check on UNIX servers which involves logging onto UNIX servers and using the grep command to check if a GID exists in the /etc/group directory e.g. grep 12345 /etc/group I have five to check on each server, is there anyway I can incorporate them into one command and... (2 Replies)
Discussion started by: @MeDaveT
2 Replies

4. UNIX for Dummies Questions & Answers

Grep in Perl - Searching through multiple files

I'm attempting to use grep in Perl with very little success. What I would like to do in Perl is get the output of the following grep code: grep -l 'pattern' * This gives me a list of all the files in a directory that contain the pattern that was searched. My attempts to do this in Perl... (4 Replies)
Discussion started by: WongSifu
4 Replies

5. Shell Programming and Scripting

Nawk help searching for multiple lines and multiple searches

I use this command to find a search (Nr of active alarms are) and print one line before and 10 lines after the search keywords. nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r;print;c=a}b{r=$0}' b=1 a=10 s="Nr of active alarms are:" *.log However, I would like to know how to tell it to print... (3 Replies)
Discussion started by: tthach830
3 Replies

6. Shell Programming and Scripting

grep searching interval

Hi all, I just want to find all values that are in a specified interval. I tryed it with grep e- file , it does not work. Is it possible to get values wich are lower a special number, like grep >e-18 file? Thanks a lot (4 Replies)
Discussion started by: newcommer
4 Replies

7. Shell Programming and Scripting

grep searching

I am making a script but having little problem. at one part I need to find one number format or other format from a file.. those formats are xxx-xx-xxxx or xxxxxxxxx i tried grep '( \{3\}-\{2\}-\{3\} |\{9\})' if i do them sepratly it work but like this it is not working Please check... (7 Replies)
Discussion started by: Learnerabc
7 Replies

8. Shell Programming and Scripting

searching using grep command

Hi, i have a file called alert_pindb.log i need to grep and count for all the lines starting with "ORA-" but i need to exclude the line which is having "ORA-00600 " i am using following syntax to count the ORA- nos "grep \"ORA-\" alert_pindb.log | wc -l"; since ORA- may be anything... (9 Replies)
Discussion started by: prakash.gr
9 Replies

9. Shell Programming and Scripting

GREP Searching for a newbie...

Hi, I really need some help with GREP searching... I need to find all occurances of a file reference and remove two characters from the end of the reference. For example, here are a few lines showing the text: <image file="STRAIGHT_004CR.jpg" ALT="STRAIGHT_004CR.jpg" /> <image... (8 Replies)
Discussion started by: steveglevin
8 Replies

10. Shell Programming and Scripting

Searching multiple files with multiple expressions

I am using a DEC ALPHA running Digital UNIX (formly DEC OSF/1) and ksh. I have a directory with hundreds of files that only share the extension .rpt. I would like to search that directory based on serial number and operation number and only files that meet both requirements to be printed out. I... (6 Replies)
Discussion started by: Anahka
6 Replies
Login or Register to Ask a Question