Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

Thread: grep wont search multiple patterns sepperated by a newline

  1. #11
    Join Date
    Nov 2007
    Location
    London, England
    Beans
    7,702

    Re: grep wont search multiple patterns sepperated by a newline

    Quote Originally Posted by jcdenton1995 View Post
    I thought that wrapping a string in quotes was sufficient to protect the meta-characters in it from being expanded by the shell? why is the '$' necessary? (or does the '$' ensure that '\n' is expanded by the shell??)
    Exactly that. "man bash" is really hard to read unless you know what you are looking for. Try "man bash" and search for QUOTING (yes, capitals). There is this sentence:
    Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard.

  2. #12
    Join Date
    Dec 2014
    Beans
    2,585

    Re: grep wont search multiple patterns sepperated by a newline

    From 'man bash', section "QUOTING":
    ... Words of the form $'string' are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. ....
    So, yes, the '$' makes the bash expand '\n' to a newline-character. (Don't know how often I've read that page over the years and never noticed this construct ...)

    I don't have backslashes before the newlines in my example because those would make grep give me an error:"grep: Angehängter Backslash (»\«)" ('appended backslash'). Without the backslash your example would search for the patterns 'owl' and '*****-cat' in the file 'theowlandthepussycat'. If those aren't in there, you'd get no output.

    Holger

  3. #13
    Join Date
    Mar 2010
    Location
    Squidbilly-Land
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: grep wont search multiple patterns sepperated by a newline

    Quote Originally Posted by jcdenton1995 View Post
    This actually works! it was the '$' that I was missing. I thought that wrapping a string in quotes was sufficient to protect the meta-characters in it from being expanded by the shell? why is the '$' necessary? (or does the '$' ensure that '\n' is expanded by the shell??)
    Perhaps I missed it, but this is a bash-specific thing. Other shells would do it different. Bash expansion (regex/globbing) is mostly intuitive, until it isn't.

    If it were me, I'd use egrep (the same as grep -E) which is for 'enhanced regex grep'. 2 fewer characters to type, 'eg{tab}' does the command completion fast.
    Code:
           -E, --extended-regexp
                  Interpret PATTERNS as extended regular  expressions  (EREs,  see
                  below).
    
           -P, --perl-regexp
                  Interpret  PATTERNS  as  Perl-compatible   regular   expressions
                  (PCREs).   This option is experimental when combined with the -z
                  (--null-data) option, and grep  -P  may  warn  of  unimplemented
                  features.
    This basically results in nearly python/perl regex handling, so there are fewer surprises. I know perl, so those regex are sometime needed, -P option, but that is seldom, since whenever a bash script gets over 1 page in length, I switch to perl for all the added capabilities like multi-line regex codes with comments. Perl hasn't been write-only in a very long time.

    If multiple regex are needed, consider using multiple -e options, one for each.

  4. #14
    Join Date
    Apr 2020
    Location
    where the work takes me
    Beans
    246
    Distro
    Ubuntu 20.04 Focal Fossa

    Re: grep wont search multiple patterns sepperated by a newline

    Quote Originally Posted by Holger_Gehrke View Post
    From 'man bash', section "QUOTING":

    So, yes, the '$' makes the bash expand '\n' to a newline-character. (Don't know how often I've read that page over the years and never noticed this construct ...)

    I don't have backslashes before the newlines in my example because those would make grep give me an error:"grep: Angehängter Backslash (»\«)" ('appended backslash'). Without the backslash your example would search for the patterns 'owl' and '*****-cat' in the file 'theowlandthepussycat'. If those aren't in there, you'd get no output.

    Holger
    Righhht now I've got it, I never realised you can just hit enter after typing the first pattern to move to a newline in the terminal (naturally I assumed this would just attempt to run an incomplete command), so is that specifically a function of grep, that it 'knows' when your giving pattern arguments, if you hit enter it should simply move to a new line?

    Anyway we've established that one can search for multiple patterns in the following three ways...
    Code:
    grep $'[PATTERN1]\n[PATTERN2]\n[PATTERN3]' [INPUTFILE]
    
    
    grep '[PATTERN1]
    [PATTERN2]
    [PATTERN3]' [INPUTFILE]
    
    
    grep -E '[PATTERN1]|[PATTERN2]|[PATTERN3]' [INPUTFILE]
    So that pretty much clears it up, thank you all for your help!

  5. #15
    Join Date
    Mar 2010
    Location
    Squidbilly-Land
    Beans
    Hidden!
    Distro
    Ubuntu

    Re: grep wont search multiple patterns sepperated by a newline

    This should work too:
    Code:
    grep -E -e [PATTERN1] -e [PATTERN2] -e [PATTERN3]  [INPUTFILE]
    or if you want them on separate lines:
    Code:
    grep -E -e [PATTERN1] \
    -e [PATTERN2] \
    -e [PATTERN3]  \
    [INPUTFILE]
    Just ensure there isn't any whitespace after the normal line continuation character \.

  6. #16
    Join Date
    Dec 2014
    Beans
    2,585

    Re: grep wont search multiple patterns sepperated by a newline

    Quote Originally Posted by jcdenton1995 View Post
    Righhht now I've got it, I never realised you can just hit enter after typing the first pattern to move to a newline in the terminal (naturally I assumed this would just attempt to run an incomplete command), so is that specifically a function of grep, that it 'knows' when your giving pattern arguments, if you hit enter it should simply move to a new line?
    It's not a function of grep, grep isn't running yet. It's the shell (or probably the readline library that the shell uses for text entry) that recognizes the odd number of quotes as a sign that there's more to come.

    Holger

Page 2 of 2 FirstFirst 12

Bookmarks

Posting Permissions

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