Thread: Question on grepping multiple words in a line

  1. #1
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329

    Question on grepping multiple words in a line

    I'm trying have grep both the words 'cdalten' and "bash'. The closest I got was being able to grep either one of them like below.

    [cdalten@localhost ~]$ w | grep -E 'cdalten|bash'
    cdalten pts/1 :0.0 05Apr09 6days 0.36s 0.36s bash
    cdalten pts/2 :0.0 05Apr09 5days 2.24s 1.88s python
    cdalten pts/3 :0.0 16:11 7:55 0.64s 0.13s python
    cdalten pts/4 :0.0 17:05 0.00s 0.49s 0.03s w
    cdalten pts/5 :0.0 Thu19 32:14m 0.89s 0.89s bash
    What's the magical regex expression that I pass to grep so that I get the following instead

    cdalten pts/1 :0.0 05Apr09 6days 0.36s 0.36s bash
    cdalten pts/5 :0.0 Thu19 32:14m 0.89s 0.89s bash

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    Code:
    "[cdalten|bash]"
    ?

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I generally do this by running "grep first | grep second", but I'm generally lazy when it comes to learning advanced expressions for tools that can easily be done by typing a bit more [I generally use this mostly when I find that the first grep got me far to much, and it's often easier to just add a "| grep somethingelse" onto the existing line, than it is to reform the expression (and you can use "grep -v someword" to only accept lines that do NOT contain someword - e.g. list all processes NOT bash: "w | grep cdalten | grep -v bash").

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    I think he meant "A OR B" not "A AND B".

    [edit]Oh nvm, I think you are right.[/edit]

  5. #5
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Quote Originally Posted by cyberfish View Post
    I think he meant "A OR B" not "A AND B".

    [edit]Oh nvm, I think you are right.[/edit]
    Yeah, I want grep to return if both 'A AND B' are true

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Code:
    cat txt | grep 'bash' | grep 'cdalten'
    There is no advantage to using a regular expression with this, but if you want to, there's always
    Code:
    cat txt | perl -ne 'if ((/bash/) && (/cdalten/)) {print $_}'
    This one works too but is sensitive to whitespace at the end of line:
    Code:
    cat txt | perl -ne 'if (/^cdalten.*bash$/) {print $_}'
    Last edited by MK27; 04-12-2009 at 11:43 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Regular languages support conjunction, but I've never seen it in any regex syntax I've used. I do the same thing matsp does -- two greps.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help again with scrolling without wrapping
    By Dukefrukem in forum C Programming
    Replies: 8
    Last Post: 09-21-2007, 12:48 PM
  2. command line wildcard question
    By McFury in forum C Programming
    Replies: 1
    Last Post: 09-12-2007, 12:42 PM
  3. Command Line arguments question
    By micpi in forum C Programming
    Replies: 7
    Last Post: 04-24-2007, 08:46 PM
  4. Replies: 1
    Last Post: 05-01-2003, 02:52 PM
  5. Multiple Command Line Args
    By mart_man00 in forum C Programming
    Replies: 10
    Last Post: 08-16-2002, 02:15 PM