LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-18-2014, 09:22 AM   #1
totemtommie
LQ Newbie
 
Registered: Jul 2014
Posts: 11

Rep: Reputation: Disabled
grep or egrep


Hi

I have lines containing: elephant, tiger, elephant AND tiger.

I want to grep the line with both AND use variables (STRING1=elephant STRING2=tiger) AND search in a whole directory

egrep "($STRING1|$STRING2)" /tmp/grep1.txt gives me all 3 lines and only works if file is specified

grep -e "$STRING1"-e "$STRING2" /tmp/grep1 gives me all 3 lines and only works if file is specified

egrep -r 'elephant|tiger' /tmp gives me all 3 lines but DOES work in whole directory. But if I replace elephant and tiger with variables it doest work anymore

i'm lost!
 
Old 07-18-2014, 09:31 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,295

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Hi

Variables inside single quotes are not converted to values. Try double quotes instead.

egrep -r "$STRING1|$STRING2" /tmp
 
Old 07-18-2014, 09:35 AM   #3
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,480

Rep: Reputation: 420Reputation: 420Reputation: 420Reputation: 420Reputation: 420
grep accepts -r option too
 
Old 07-18-2014, 09:37 AM   #4
totemtommie
LQ Newbie
 
Registered: Jul 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
okey great this script does indeed run. But in the results I find all 3 lines. How can you grep on BOTH string 1 and 2?
 
Old 07-18-2014, 10:09 AM   #5
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,480

Rep: Reputation: 420Reputation: 420Reputation: 420Reputation: 420Reputation: 420
You mean match lines with both $STRING1 & $STRING2?

If $STRING1 is always before $STRING2
Code:
grep -r "$STRING1.*$STRING2" /tmp
Or if order can differ:
Code:
grep -r -e "$STRING1.*$STRING2" -e "$STRING2.*$STRING1" /tmp
 
Old 07-21-2014, 03:32 AM   #6
totemtommie
LQ Newbie
 
Registered: Jul 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
Thanks a lot! this is exactly what I need:-)
 
Old 07-23-2014, 02:57 PM   #7
bulrush15
LQ Newbie
 
Registered: Jul 2014
Location: West Michigan
Posts: 29

Rep: Reputation: Disabled
Quote:
Originally Posted by Guttorm View Post
Hi

Variables inside single quotes are not converted to values. Try double quotes instead.

egrep -r "$STRING1|$STRING2" /tmp

Huh. Just like Perl. Is this true only for certain shells? Or just bash?
 
Old 07-24-2014, 07:41 AM   #8
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
It should be a qn of the version of the tool ie egrep in this case in terms of the regex.
OTOH, interpolation of vars within double-quotes is fairly universal in shells (I pass on csh & derivatives though ..)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] grep or egrep help roopakl Linux - Newbie 3 12-19-2012 12:12 PM
how to do Logical AND with grep or egrep amit_pansuria Programming 2 08-09-2009 03:39 AM
Help with ls and grep/egrep kasthana Linux - Newbie 1 05-29-2008 02:06 PM
using grep and egrep in the terminal KumARan23 Linux - Newbie 3 11-11-2007 10:27 AM
Using Grep and Egrep linux-nerd Linux - General 5 10-10-2004 12:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 12:17 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration