LinuxQuestions.org
Review your favorite Linux distribution.
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
 
LinkBack Search this Thread
Old 05-26-2011, 01:09 AM   #1
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,345

Rep: Reputation: 135Reputation: 135
searching words with space


I want to search lines with these stings with one or more spaces..

e.g

Code:
a b
a  b
a   b
a    b
How will I achieve this by egrep ?

Last edited by divyashree; 05-26-2011 at 04:40 AM.
 
Old 05-26-2011, 01:20 AM   #2
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
Code:
foo$ man egrep
foo$ cat infile
ab
a b
a  b
a--b
a   b
a    b
aaabbb
foo$ egrep 'a +b' infile
a b
a  b
a   b
a    b
foo$
 
Old 05-26-2011, 01:44 AM   #3
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,345

Original Poster
Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by Telengard View Post
Code:
foo$ man egrep
foo$ cat infile
ab
a b
a  b
a--b
a   b
a    b
aaabbb
foo$ egrep 'a +b' infile
a b
a  b
a   b
a    b
foo$
No not searching...

I have two variables a and b.

ad I want to search all the lines:
with '$a +$b'

Last edited by divyashree; 05-26-2011 at 01:53 AM.
 
Old 05-26-2011, 01:52 AM   #4
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
Quote:
Originally Posted by divyashree View Post
I want to search lines
Quote:
Originally Posted by divyashree View Post
No not searching...
Sorry, I don't understand what you mean.

Please provide example input and example output, and be more specific about exactly what you want to do.
 
Old 05-26-2011, 03:45 AM   #5
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,345

Original Poster
Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by Telengard View Post
Sorry, I don't understand what you mean.

Please provide example input and example output, and be more specific about exactly what you want to do.
I want like this:

I have two variables a and b.

I want to search all the lines containing the value of a and b with one or more space between them...

Code:
'$a +$b'
While doing
PHP Code:
egrep -'$a +$b' <file
it returns nothing..
 
Old 05-26-2011, 05:43 AM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,193

Rep: Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785
Well assuming you want the values of a and b to be expanded you will need to use double quotes.
 
1 members found this post helpful.
Old 05-26-2011, 06:11 AM   #7
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,345

Original Poster
Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by grail View Post
Well assuming you want the values of a and b to be expanded you will need to use double quotes.
I tried with both single quotes and double quotes... yet no success
 
Old 05-26-2011, 06:15 AM   #8
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,193

Rep: Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785
Then I would need to see an example of an input file and the proposed values for a and b?
 
1 members found this post helpful.
Old 05-26-2011, 11:09 PM   #9
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,345

Original Poster
Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by grail View Post
Then I would need to see an example of an input file and the proposed values for a and b?
lets the input is from "ls -l" where $a stores the month and $b stores the date, when date is single digit there are 2 spaces between $a and $b but one space while date is 2digit.
 
Old 05-27-2011, 12:10 AM   #10
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5
Posts: 16,086

Rep: Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995
If(!) I understand you, a quick n dirty soln is
Code:
grep a file|grep b
 
Old 05-27-2011, 01:09 AM   #11
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,193

Rep: Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785
Well parsing ls values would not be my first suggestion for reasons shown here.
If you must though you could use awk as it will handle the spaces for you.

Lastly, your explanation is unclear as month is a part of date so a would already be a subset of b, ie they would not be beside each other.
 
1 members found this post helpful.
Old 05-27-2011, 03:03 AM   #12
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,345

Original Poster
Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by chrism01 View Post
If(!) I understand you, a quick n dirty soln is
Code:
grep a file|grep b
It can work in some situations but not in most of the cases in, my case also...

I want to search the combination ...

Last edited by divyashree; 05-27-2011 at 03:09 AM.
 
Old 05-27-2011, 11:48 PM   #13
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5
Posts: 16,086

Rep: Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995Reputation: 1995
According to your OP, that would work; you only get lines that have both a and b in them, separated by any amt of space
 
Old 05-28-2011, 12:42 AM   #14
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,193

Rep: Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785Reputation: 1785
Quote:
you only get lines that have both a and b in them, separated by any amt of space
Unfortunately Chris I would have to disagree here. If the input is from ls -l being captured in a file:
Code:
-rw-r--r-- 1 grail grail 0 Jan 28/01/2011 May
-rw-r--r-- 1 grail grail 0 May 28/05/2011 October
If we assume that a and b have the following values:
Code:
a=May
b=28
By looking at the data, and knowing we want a and b separated by a space, only the October file should be returned, but using:
Code:
grep a file|grep b
the May file will also be returned.

@OP - have you tried my suggestion of either using awk or putting double quotes around your example:
Code:
egrep -i "$a +$b" <file>
 
1 members found this post helpful.
Old 05-28-2011, 01:28 AM   #15
divyashree
Senior Member
 
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,UBUNTU
Posts: 1,345

Original Poster
Rep: Reputation: 135Reputation: 135
Quote:
Originally Posted by grail View Post
Unfortunately Chris I would have to disagree here. If the input is from ls -l being captured in a file:
Code:
-rw-r--r-- 1 grail grail 0 Jan 28/01/2011 May
-rw-r--r-- 1 grail grail 0 May 28/05/2011 October
If we assume that a and b have the following values:
Code:
a=May
b=28
By looking at the data, and knowing we want a and b separated by a space, only the October file should be returned, but using:
Code:
grep a file|grep b
the May file will also be returned.

@OP - have you tried my suggestion of either using awk or putting double quotes around your example:
Code:
egrep -i "$a +$b" <file>
Yes I tried with doublequotes. Not worked.

Suppose my input contains:
Code:
drwxr-xr-x  2 root root 4096 Feb  2 11:23 79
drwxr-xr-x  2 root root 4096 Feb 12 11:11 8
In my case, in a script, the
Code:
ls -l | egrep -i "$a +$b"
is written.

If I use

Code:
grep $a file|grep $b
then..
let
a=Feb
b=2

I want lines with 2nd feb...
then the above both lines will return..
 
  


Reply

Tags
egrep, regular expression


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Division of Logical Memory Space in to User Space and Kernel Space shreshtha Linux - Newbie 2 01-14-2010 09:59 AM
Do we have any chance of calling user space callback function from kernel space? ravishankar.g Linux - Newbie 1 09-22-2009 07:14 PM
how to call socket prog code written in user space from kernel space???HELP kurt2 Programming 2 07-15-2009 09:56 PM
Searching for space scbops Linux - Hardware 3 09-26-2008 01:29 PM
LXer: Worms in space: NASA confirms International Space Station infected LXer Syndicated Linux News 0 08-28-2008 09:40 AM


All times are GMT -5. The time now is 06:46 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration