Searching for multiple patterns in files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Searching for multiple patterns in files
# 1  
Old 02-25-2010
Searching for multiple patterns in files

I have a situation where I need to search for multiple strings (error messages) such as 'aborted' 'file not found' etc in directory having logs. I have put all the error messages in a text file and using the command.
Code:
grep -f <textfile> <filetobegrepped>

I'm doing this thru a script where I have a loop which will get the file name to be grepped and search for the messages. If ound I need to display the file which contains the pattern.
During exeuction I have the error as follows:
Code:
Usage: grep -hblcnsviw pattern file

Can somebody help what could be wrong here..

This is the piece of code
Code:
for FILE in *; do
   if [ -d $FILE ]
   then
      echo "This is a directory. Search not required"
   else
      grep -f messages.txt $FILE
      if [ $? = 0 ]
      then
         echo "Please check $FILE for errors" >> errorrep
      fi
   fi
done


Last edited by zaxxon; 02-25-2010 at 05:10 AM.. Reason: use code tags please, ty
# 2  
Old 02-25-2010
grep does not check directories for patterns, only files. It does only descent and do recursive searches when using -R, which you don't do anyway in your example.
So you can spare out all those if/then/else/fi tests.

Also you don't have to check all files inside a for loop since grep does this on it's own when you specify a wildcard as parameter.

To see the name of the file that contains the pattern you got in your list, you can add a -l to grep.

So try something like this while standing in the directory where the supposed files are - else add some path to the wildcard accordingly:
Code:
grep -lf mypatternlist.txt *


Last edited by zaxxon; 02-25-2010 at 05:25 AM.. Reason: typo
# 3  
Old 02-25-2010
This code is working fine in my system ,
hopefully some unexpected thing would be in the message.txt ,
can u give some idea about the file messages.txt , can u paste some lines of messages.txt
here
# 4  
Old 02-25-2010
At our installation, the -f option is not working.
# 5  
Old 02-25-2010
A solution with awk:
Code:
awk '
BEGIN{
  while ((getline < "textfile") > 0) {
    a[++i]=$0
  }
}
{
  for(i in a) {
    if($0 ~ a[i]) {
      print
      next
    }
  }
}' filetobegrepped >> errorrep

# 6  
Old 02-25-2010
Possible need to use a differrent grep program?

grep -e doesn't work on solaris
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Searching multiple patterns and construct SQL

Hello, I have attached 2 files 1) Original_table_definition.txt => which has definition of 3 tables 2) describe_table_output.txt => which has again 3 tables definition gotten thorugh doing a show table or describe table way. Now difference between 3 tables are that tablea has no... (2 Replies)
Discussion started by: nv186000
2 Replies

2. UNIX for Dummies Questions & Answers

[Solved] List Files With Multiple Name Patterns

Hi, We have created a script that would accept the an indicator as a parameter and archive files present in a directory. The indicator would drive what the name pattern of the files to be archived should be. If the indicator is 1, then the pattern to look out for is FACT*. If the indicator is... (2 Replies)
Discussion started by: jerome_rajan
2 Replies

3. Shell Programming and Scripting

Searching multiple patterns using awk

Hello, I have the following input file: qh1adm 20130710111201 : tp import all QH1 u6 -Dsourcesystems=BFI,EBJ qh1adm 20130711151154 : tp import all QH1 u6 -Dsourcesystems=BFI,EBJ qx1adm 20130711151154 : tp count QX1 u6 -Dsourcesystems=B17,E17,EE7 qh1adm 20130711151155 : tp import all... (7 Replies)
Discussion started by: kcboy
7 Replies

4. Shell Programming and Scripting

searching multiple patterns in perl

Hi, I have code like: Output it is comming as: Rels: WM2 Rels: WG2 Rels: 5 - pre/prods.pl Rels: 6 Rels: 7 Rels: 8 Rels: 10 Rels: Int But i want only "Rels: 5" pattern Just above "- pre/prods.pl". By... (7 Replies)
Discussion started by: Anjan1
7 Replies

5. Shell Programming and Scripting

Search multiple patterns in multiple files

Hi, I have to write one script that has to search a list of numbers in certain zipped files. For eg. one file file1.txt contains the numbers. File1.txt contains 5,00,000 numbers and I have to search each number in zipped files(The number of zipped files are around 1000 each file is 5 MB) I have... (10 Replies)
Discussion started by: vsachan
10 Replies

6. Shell Programming and Scripting

Searching for multiple patterns in a file

Hi All, I have a file in which i have to search for a pattern from the beginning of the file and if the pattern is found , then i have to perform a reverse search from that line to the beginning of the file to get the first occurrence of another pattern. sample input file hey what are you... (8 Replies)
Discussion started by: Kesavan
8 Replies

7. Shell Programming and Scripting

grep multiple patterns in number of files

Hi, I want to list the files containing a no of pattern like for single string i can use grep -l "string" * This command will enlist the files containg this string. Similarly i would like to use for multiple string. I like to enlist file names having string1 and string 2 Can... (3 Replies)
Discussion started by: vikash_k
3 Replies

8. Shell Programming and Scripting

Searching patterns in 1 file and deleting all lines with those patterns in 2nd file

Hi Gurus, I have a file say for ex. file1 which has 3500 lines in it which are different account numbers and another file (file2) which has 230000 lines in it. I want to read all the lines in file1 and delete all those lines from file2 which has that same pattern as in file1. I am not quite... (4 Replies)
Discussion started by: toms
4 Replies

9. Shell Programming and Scripting

Script for searching a pattern in 5 files and deleting the patterns given

Hi All, I have written the below script that searches for the pattern in a file and delete them if present. please can some one have a look and suggest the changes in the script. #!bin/sh # The pattern that user want to add to the files echo "Enter the pattern of the redirect" read... (4 Replies)
Discussion started by: Shazin
4 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