awk or grep to match # of words before and after pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk or grep to match # of words before and after pattern
# 1  
Old 01-23-2012
awk or grep to match # of words before and after pattern

Pipe binary file matches grep results to file
I am using grep to match a pattern, but the output is strange.


Code:
$ grep -r -o "pattern" *

Gives me:

Code:
Binary file foo1 matches
Binary file foo2 matches
Binary file foo3 matches

To find the lines before/after, I then have to use the following on each file:

Code:
$ strings foo1 | grep -A1 -B1 "pattern"

How can I recursively perform this operation with strings? Or, is there an alternative?

Last edited by chipperuga; 01-23-2012 at 03:01 PM..
# 2  
Old 01-25-2012
This should handle the recursive 'strings'
Code:
find . -type f | while read f
do
   echo ====$f====
   strings $f | grep -A1 -B1 "pattern"
   echo
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep everything between two pattern if match not found

I need to help to work this Print everything between 2 patterns if grep is not found the search word example Detroit orange cat bat rat apple sed -n "/Detroit,/apple/p" d |grep chicago output would be Detroit orange cat bat rat (1 Reply)
Discussion started by: jhonnyrip
1 Replies

2. Shell Programming and Scripting

sed and awk usage to grep a pattern 1 and with reference to this grep a pattern 2 and pattern 3

Hi , I have a file where i have modifed certain things compared to original file . The difference of the original file and modified file is as follows. # diff mir_lex.c.modified mir_lex.c.orig 3209c3209 < if(yy_current_buffer -> yy_is_our_buffer == 0) { --- >... (5 Replies)
Discussion started by: breezevinay
5 Replies

3. Shell Programming and Scripting

Grep two words with exact match

HI Input : Counters Counter Int Ints Counters Counters Ints Ints I want to grep Counter|Int Output : Counter (1 Reply)
Discussion started by: pareshkp
1 Replies

4. UNIX for Dummies Questions & Answers

Match Pattern after certain pattern and Print words next to Pattern

Hi experts , im new to Unix,AWK ,and im just not able to get this right. I need to match for some patterns if it matches I need to print the next few words to it.. I have only three such conditions to match… But I need to print only those words that comes after satisfying the first condition..... (2 Replies)
Discussion started by: 100bees
2 Replies

5. Shell Programming and Scripting

Awk to match a pattern and perform a search after the first pattern

Hello Guyz I have been following this forum for a while and the solutions provided are super useful. I currently have a scenario where i need to search for a pattern and start searching by keeping the first pattern as a baseline ABC DEF LMN EFG HIJ LMN OPQ In the above text i need to... (8 Replies)
Discussion started by: RickCharles
8 Replies

6. Shell Programming and Scripting

unable to match grep pattern

Hi All, I am trying to select all files in a directory which are not with .gz extension . for which I am using below script , but its rejecting both .gz and .z extension files,as in each letter is considered separately. PFB ls -lrt | awk '{print $9}'| egrep "^IRAMS.*$" please suggest... (1 Reply)
Discussion started by: Jcpratap
1 Replies

7. Shell Programming and Scripting

AWK match $1 $2 pattern in file 1 to $1 $2 pattern in file2

Hi, I have 2 files that I have modified to basically match each other, however I want to determine what (if any) line in file 1 does not exist in file 2. I need to match column $1 and $2 as a single string in file1 to $1 and $2 in file2 as these two columns create a match. I'm stuck in an AWK... (9 Replies)
Discussion started by: right_coaster
9 Replies

8. Shell Programming and Scripting

Pattern match in grep

Hi, I try to grep lines with pattern like float_or_int float_or_int float_or_int where float_or_int is any float or any integer of any length. I tried some : grep "* * *" FILENAME #Work for integers but not for floats grep '\<.*\> \<.*\> \<.*\>' FILENAME #Work for float but not... (4 Replies)
Discussion started by: tipi
4 Replies

9. Shell Programming and Scripting

grep on multiple words to match text template

hi, I have few text templates as a simple ex: template 1 city Name: zip code: state Name: template2: employee Name: Phone number: I wish to grep on given text file and make sure the text file matches one of these templates. Please give your ideas. (6 Replies)
Discussion started by: rider29
6 Replies

10. UNIX for Dummies Questions & Answers

how to use pattern match with grep

hi, i'm having problem like this. i wish to grep some keyword from certain files in one directory. let's say i have a lot of files in my directory with the name of file.1 file.2 file.3 ...... file.500 i only wish to grep the keyword from file.20, file.21, file.23 .... file.50 i tried... (5 Replies)
Discussion started by: rei
5 Replies
Login or Register to Ask a Question