The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Grepping for two strings that MUST exist on the same line Rally_Point Shell Programming and Scripting 6 06-02-2009 12:12 AM
Grepping for Exact Strings SkySmart Shell Programming and Scripting 27 05-04-2009 02:12 PM
how to extract multiple strings from a line vin_eme Shell Programming and Scripting 3 01-22-2009 01:46 PM
Trouble grepping for multiple strings thecoffeeguy Shell Programming and Scripting 5 05-16-2008 06:53 AM
Grepping for strings t4st33@mac.com UNIX for Dummies Questions & Answers 5 06-21-2007 12:51 AM

Reply
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 06-11-2009
Rally_Point Rally_Point is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 11
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 (permalink)  
Old 06-11-2009
vidyadhar85's Avatar
vidyadhar85 vidyadhar85 is offline Forum Staff  
Moderator(The Tutor)
  
 

Join Date: Jun 2008
Location: INDIA
Posts: 1,418
you can use awk

Code:
awk '/Doe/&&(/John/||/Jonathon/){print}' * >output.txt

  #3 (permalink)  
Old 06-11-2009
Rally_Point Rally_Point is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 11
Quote:
Originally Posted by vidyadhar85 View Post
you can use awk

Code:
awk '/Doe/&&(/John/||/Jonathon/){print}' * >output.txt

Awk scares me. It looks like C code. But that looks good, I'll give it a try. Thanks!
  #4 (permalink)  
Old 06-12-2009
kaedwen kaedwen is offline
Registered User
  
 

Join Date: Jun 2009
Posts: 2
Quote:
Originally Posted by Rally_Point View Post
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 (permalink)  
Old 06-12-2009
dr.house dr.house is offline
Registered User
  
 

Join Date: Dec 2008
Location: Switzerland
Posts: 229
[deleted as redundant]

Last edited by dr.house; 06-12-2009 at 12:30 PM.. Reason: reference added
  #6 (permalink)  
Old 06-12-2009
methyl methyl is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 1,184
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!
Reply

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 10:51 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0