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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
|
05-26-2011, 01:09 AM
|
#1
|
|
Senior Member
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,
Posts: 1,261
Rep: 
|
searching words with space
I want to search lines with these stings with one or more spaces..
e.g
How will I achieve this by egrep ?
Last edited by divyashree; 05-26-2011 at 04:40 AM.
|
|
|
|
05-26-2011, 01:20 AM
|
#2
|
|
Member
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Rep: 
|
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$
|
|
|
|
05-26-2011, 01:44 AM
|
#3
|
|
Senior Member
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,
Posts: 1,261
Original Poster
Rep: 
|
Quote:
Originally Posted by Telengard
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.
|
|
|
|
05-26-2011, 01:52 AM
|
#4
|
|
Member
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Rep: 
|
Quote:
Originally Posted by divyashree
I want to search lines
|
Quote:
Originally Posted by divyashree
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.
|
|
|
|
05-26-2011, 03:45 AM
|
#5
|
|
Senior Member
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,
Posts: 1,261
Original Poster
Rep: 
|
Quote:
Originally Posted by Telengard
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...
While doing
PHP Code:
egrep -i '$a +$b' <file>
it returns nothing..
|
|
|
|
05-26-2011, 05:43 AM
|
#6
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,325
|
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.
|
05-26-2011, 06:11 AM
|
#7
|
|
Senior Member
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,
Posts: 1,261
Original Poster
Rep: 
|
Quote:
Originally Posted by grail
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
|
|
|
|
05-26-2011, 06:15 AM
|
#8
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,325
|
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.
|
05-26-2011, 11:09 PM
|
#9
|
|
Senior Member
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,
Posts: 1,261
Original Poster
Rep: 
|
Quote:
Originally Posted by grail
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.
|
|
|
|
05-27-2011, 12:10 AM
|
#10
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 15,021
|
If(!) I understand you, a quick n dirty soln is
|
|
|
|
05-27-2011, 01:09 AM
|
#11
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,325
|
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.
|
05-27-2011, 03:03 AM
|
#12
|
|
Senior Member
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,
Posts: 1,261
Original Poster
Rep: 
|
Quote:
Originally Posted by chrism01
If(!) I understand you, a quick n dirty soln is
|
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.
|
|
|
|
05-27-2011, 11:48 PM
|
#13
|
|
Guru
Registered: Aug 2004
Location: Brisbane
Distribution: Centos 6.4, Centos 5.9
Posts: 15,021
|
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
|
|
|
|
05-28-2011, 12:42 AM
|
#14
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,325
|
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:
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:
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.
|
05-28-2011, 01:28 AM
|
#15
|
|
Senior Member
Registered: Apr 2007
Location: bbsr,orissa,India
Distribution: RHEL5 ,RHEL4,CENT OS5,FEDORA,
Posts: 1,261
Original Poster
Rep: 
|
Quote:
Originally Posted by grail
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:
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:
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..
|
|
|
|
| Thread Tools |
Search 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
HTML code is Off
|
|
|
All times are GMT -5. The time now is 09:07 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|