Grepping Multiple Strings on the Same Line 'Or'


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Grepping Multiple Strings on the Same Line 'Or'
# 1  
Old 06-11-2009
Grepping Multiple Strings on the Same Line 'Or'

I've got this command that I've been using to find strings on the same line, say I'm doing a search for name:

find . -name "*" | xargs grep -i "Doe" | grep -i "John" > output.txt

This gives me every line in a file that has John and Doe in it. I'm looking to add a OR operator for the second grep statement, so that I can grep for "John" OR "Jonathon" for example. In this example, I know that I could just enter "Jo*" but I don't want any other terms like Jose or Jonas.

I tried

find . -name "*" | xargs grep -i 'Doe\|John' > output.txt

but that didn't seem to work.

Thanks in advance.
# 2  
Old 06-11-2009
you can use awk
Code:
awk '/Doe/&&(/John/||/Jonathon/){print}' * >output.txt

# 3  
Old 06-11-2009
Quote:
Originally Posted by vidyadhar85
you can use awk
Code:
awk '/Doe/&&(/John/||/Jonathon/){print}' * >output.txt


Awk scares me. It looks like C code. Smilie But that looks good, I'll give it a try. Thanks!
# 4  
Old 06-12-2009
Quote:
Originally Posted by Rally_Point
I've got this command that I've been using to find strings on the same line, say I'm doing a search for name:

find . -name "*" | xargs grep -i "Doe" | grep -i "John" > output.txt

This gives me every line in a file that has John and Doe in it. I'm looking to add a OR operator for the second grep statement, so that I can grep for "John" OR "Jonathon" for example. In this example, I know that I could just enter "Jo*" but I don't want any other terms like Jose or Jonas.
Here are a couple of options:

find . -name "*"| xargs grep -i Doe| grep -i -e John -e Jonathon >output.txt
find . -name "*"| xargs grep -i Doe| egrep -i "(John|Jonathon)" >output.txt

You can also combine the find and xargs commands:

find . -name "*" -exec grep -i Doe {} \; | grep -i -e John -e Jonathon >output.txt
# 5  
Old 06-12-2009
[deleted as redundant]

Last edited by dr.house; 06-12-2009 at 12:30 PM.. Reason: reference added
# 6  
Old 06-12-2009
Another approach. But don't have the script or the output file in the same tree as the find !

Code:
#!/bin/ksh
(
find . -type f -print | while read FILENAME
do
        echo "${FILENAME}"
        egrep -iw "Doe" "${FILENAME}"|egrep -iw "John|Jonathan"
done
) > /tmp/output


Last edited by methyl; 06-12-2009 at 02:05 PM.. Reason: Stopped script finding output file!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to pass strings from a list of strings from another file and create multiple files?

Hello Everyone , Iam a newbie to shell programming and iam reaching out if anyone can help in this :- I have two files 1) Insert.txt 2) partition_list.txt insert.txt looks like this :- insert into emp1 partition (partition_name) (a1, b2, c4, s6, d8) select a1, b2, c4, (2 Replies)
Discussion started by: nubie2linux
2 Replies

2. Shell Programming and Scripting

Extract multiple strings from line

Hello I have an output that has a string between quotes and another between square brackets on the same line. I need to extract these 2 strings Example line Device "nrst3a" attributes=(0x4) RAW SERIAL_NUMBER=SNL2 Output should look like nrst3a VD073AV1443BVW00083 I was trying with sed... (3 Replies)
Discussion started by: bombcan
3 Replies

3. Shell Programming and Scripting

Grepping multiple strings from one column

I have 3-column tab separated data that looks like the following: act of+n-a-large+vn-tell-v 0.067427 act_com of+n+n-a-large-manufacturer-n 0.129922 act-act_com-com in+n-j+vn-pass-aux-restate-v 0.364499666667 com nmod+n-j+ns-invader-n 0.527521 act_com-com obj+n-a-j+vd-contribute-v 0.091413... (2 Replies)
Discussion started by: owwow14
2 Replies

4. Shell Programming and Scripting

USING sed to remove multiple strings/words from a line

Hi I use sed comnand to remove occurance of one workd from a line. However I need to removed occurance of dufferent words in ne line. Original-1 Hi this is the END of my begining Comand sed s/"END"/"start"/g Output-1 Hi this is the start of my beginig But I have more... (9 Replies)
Discussion started by: mnassiri
9 Replies

5. Shell Programming and Scripting

Grepping for two strings that MUST exist on the same line

Trying to find a way to grep for two names on a line. Both names must appear on the same line so '|' / OR is out. So far, I'm just messing around and I've got find . -name "*" | xargs grep "Smith" Let me explain. I'm at a top level and need to know all the names of the files that... (6 Replies)
Discussion started by: Rally_Point
6 Replies

6. Shell Programming and Scripting

Grepping for Exact Strings

ok, apparently this is a very difficult question to answer based on my searches on google that came up fruitless. what i want to do is grep through a file for words that match a specified string. but the thing is, i keep getting all words in the file that have the string in them. say for... (27 Replies)
Discussion started by: SkySmart
27 Replies

7. Shell Programming and Scripting

how to extract multiple strings from a line

Hi I have the following requirement. i have the following line from a log file one : two : Three : four : five : six : seven : eight :nine :ten Now can you pls help what i should do to get only the following output from the above line two : five : six : seven : Eight appreciate your... (3 Replies)
Discussion started by: vin_eme
3 Replies

8. Shell Programming and Scripting

Grepping using multiple wildcards

Is there anyway you can grep using multiple wildcards? When I run the below line the results return fine; grep 12345 /usr/local/production/soccermatchplus/distributor/clients/*/out/fixtures.xml | awk -F/ '{print $8}' However ideally, I need it to grep for; grep 12345... (3 Replies)
Discussion started by: JayC89
3 Replies

9. Shell Programming and Scripting

Trouble grepping for multiple strings

I am having a heck of a time trying to write a script that will grep for multiple strings in a single file. I am really at my wits end here and I am hoping to get some feedback here. Basic information: OS: Solaris 9 Shell: KSH Oracle Database server I was trying to grep through a file... (5 Replies)
Discussion started by: thecoffeeguy
5 Replies

10. UNIX for Dummies Questions & Answers

Grepping for strings

Hello. I have a dir of 1500+ dir. In these dirs is a file host, with a tag <x_tag>. I need to : 1. grep for all dir that contain this host file that contain <x_tag> 2. print a list of these host files containing <x_tag> is this better to egrep this? (5 Replies)
Discussion started by: t4st33@mac.com
5 Replies
Login or Register to Ask a Question