Results 1 to 6 of 6
I try to find with Grep lines that contain both of two characters. For example the letter 'a' and also 'p' regardless of place on the line.
grep(1): print lines ...
- 06-30-2008 #1Just Joined!
- Join Date
- Jun 2008
- Posts
- 9
grep and 'Pattern'
I try to find with Grep lines that contain both of two characters. For example the letter 'a' and also 'p' regardless of place on the line.
So what qualifies as PATERN? Where does grep define what qualifies as 'pattern' in case the -E is not specified? Can we use BOOLEAN logic? Does it depend on the shell that is used? Or does grep evaluate the expression all by itself?grep(1): print lines matching pattern - Linux man page
grep [options] PATTERN [FILE...]
grep, egrep, fgrep - print lines matching a pattern
Example of what I want:
e.g. four files in the current directory:
pap1.txt
foo2.bar
foo3.png
foo4.test
The two lines below do not work:
I want only to use grep once.Code:ls | grep -E "a & p" ls | grep "a & p"
How can I make a logical AND?
I have read:
- 07-01-2008 #2
you can use grep as follows
but here it will match words like apple (where a comes before p) and not words like parent (where p comes first).Code:ls | grep "a*p"
to solve this you can do
Code:ls | grep a | grep p
Linux and me it's a love story
- 07-01-2008 #3Just Joined!
- Join Date
- Jun 2008
- Posts
- 9
Thank you! I've still this question:
Is logical OR possible with normal grep? (without the -E option) What is the definition of pattern?
In the mean time, this answer "a*p" pushed me in the right direction. Because the logical OR between "a*p" and "p*a" is the same as a logical AND betwen 'a' and 'p'.
For -E option the syntax is: a+p, the '+' that the preceding charcter occurs at least once. See examples below.
Code:root@erver:/home/user# ps -A | grep -E "i+f" 28 ? 00:00:00 kacpi_notify 12544 ? 00:00:00 update-notifier 12659 ? 00:00:02 notification-da root@erver:/home/user# ps -A | grep -E "f+i" 12544 ? 00:00:00 update-notifier 12659 ? 00:00:02 notification-da 12983 ? 00:00:00 soffice 12999 ? 00:00:30 soffice.bin 13252 ? 00:00:00 firefox 13268 ? 00:42:37 firefox-bin root@erver:/home/user# ps -A | grep -E "i+f|f+i" 28 ? 00:00:00 kacpi_notify 12544 ? 00:00:00 update-notifier 12659 ? 00:00:02 notification-da 12983 ? 00:00:00 soffice 12999 ? 00:00:30 soffice.bin 13252 ? 00:00:00 firefox 13268 ? 00:42:37 firefox-bin
- 07-01-2008 #4Linux Enthusiast
- Join Date
- Apr 2004
- Location
- UK
- Posts
- 658
You can specify multiple patterns to match with the -e switch.
Let us know how you get on,Code:chris@angua:~/dev/scratch$ cat test.txt a b c abc chris@angua:~/dev/scratch$ grep -e a -e b test.txt a b abc
Chris...To be good, you must first be bad. "Newbie" is a rank, not a slight.
- 07-02-2008 #5Linux User
- Join Date
- Jun 2007
- Posts
- 318
It would also match 'pple' because the "a*" means zero or more occurances of "a". The correct pattern would be "a.*p" which means "a" followed by zero or more occurances of any character followed by "p".
Using extended expression you can do it this was:
Code:ls | grep -E "(a.*p|p.*a)"
- 07-02-2008 #6
vsemaska,
thanx for the remarks.
for the first one , my bad . you are totally right.
for the second one the author of the thread said
in case the -E is not specifiedLinux and me it's a love story


Reply With Quote
