Find the answer to your Linux question:
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 ...
  1. #1
    Just 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.

    grep(1): print lines matching pattern - Linux man page
    grep [options] PATTERN [FILE...]
    grep, egrep, fgrep - print lines matching a pattern
    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?

    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:
    Code:
    ls  | grep -E "a & p"
    ls | grep "a & p"
    I want only to use grep once.
    How can I make a logical AND?

    I have read:

  2. #2
    Linux Engineer khafa's Avatar
    Join Date
    Apr 2008
    Location
    Tokyo, Japan
    Posts
    858
    you can use grep as follows
    Code:
    ls | grep "a*p"
    but here it will match words like apple (where a comes before p) and not words like parent (where p comes first).
    to solve this you can do
    Code:
    ls | grep a | grep p
    Linux and me it's a love story

  3. #3
    Just 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

  4. #4
    Linux Enthusiast
    Join Date
    Apr 2004
    Location
    UK
    Posts
    658
    You can specify multiple patterns to match with the -e switch.

    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
    Let us know how you get on,

    Chris...
    To be good, you must first be bad. "Newbie" is a rank, not a slight.

  5. #5
    Linux User
    Join Date
    Jun 2007
    Posts
    318
    Quote Originally Posted by khafa View Post
    you can use grep as follows
    Code:
    ls | grep "a*p"
    but here it will match words like apple (where a comes before p) and not words like parent (where p comes first).
    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".

    Quote Originally Posted by khafa View Post
    to solve this you can do
    Code:
    ls | grep a | grep p
    Using extended expression you can do it this was:

    Code:
    ls | grep -E "(a.*p|p.*a)"

  6. #6
    Linux Engineer khafa's Avatar
    Join Date
    Apr 2008
    Location
    Tokyo, Japan
    Posts
    858
    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 specified
    Linux and me it's a love story

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...