FAQ
Hello,

According to the grep manual page I can use the -P option to use Perl
regular expressions as follows:

grep -P PERL_REGEX INPUT_FILE

however, I cannot get the following pattern to match a literal dollar sign:

grep -P makan\$ file.txt

when I know this pattern is in the file. It matches the shorter string
without the dollar sign if I leave \$ out.

Thanks,

Jon

Search Discussions

  • Shawn H Corey at Sep 3, 2011 at 9:16 pm

    On 11-09-03 05:01 PM, Jon Forsyth wrote:
    Hello,

    According to the grep manual page I can use the -P option to use Perl
    regular expressions as follows:

    grep -P PERL_REGEX INPUT_FILE

    however, I cannot get the following pattern to match a literal dollar sign:

    grep -P makan\$ file.txt
    # You have to get the backslash past the shell

    grep -P 'makan\$' file.txt

    # or

    grep -P makan\\$ file.txt

    when I know this pattern is in the file. It matches the shorter string
    without the dollar sign if I leave \$ out.

    Thanks,

    Jon

    --
    Just my 0.00000002 million dollars worth,
    Shawn

    Confusion is the first step of understanding.

    Programming is as much about organization and communication
    as it is about coding.

    The secret to great software: Fail early & often.

    Eliminate software piracy: use only FLOSS.

    "Make something worthwhile." -- Dear Hunter
  • Shlomi Fish at Sep 4, 2011 at 6:35 am
    Hi all,

    On Sat, 03 Sep 2011 17:15:51 -0400
    Shawn H Corey wrote:
    On 11-09-03 05:01 PM, Jon Forsyth wrote:
    Hello,

    According to the grep manual page I can use the -P option to use Perl
    regular expressions as follows:

    grep -P PERL_REGEX INPUT_FILE

    however, I cannot get the following pattern to match a literal dollar sign:

    grep -P makan\$ file.txt
    # You have to get the backslash past the shell

    grep -P 'makan\$' file.txt

    # or

    grep -P makan\\$ file.txt
    Just to be a bit more pedantic, in this case:

    grep -P makan\\\$ file.txt

    would be safer and more idiomatic, because otherwise the shell would treat the
    string as a literal backslash followed by the opening of a shell variable,
    which all start with "$".

    Regards,

    Shlomi Fish


    --
    -----------------------------------------------------------------
    Shlomi Fish http://www.shlomifish.org/
    Best Introductory Programming Language - http://shlom.in/intro-lang

    I might be mad. But I’m a mad genius.

    Please reply to list if it's a mailing list post - http://shlom.in/reply .
  • Uri Guttman at Sep 3, 2011 at 9:18 pm
    "JF" == Jon Forsyth writes:
    JF> According to the grep manual page I can use the -P option to use
    JF> Perl regular expressions as follows:

    JF> grep -P PERL_REGEX INPUT_FILE

    just to let you know, nothing but perl can run perl regexes. all the
    ones that claim it are doing subsets and in some cases not even close
    anymore.

    JF> however, I cannot get the following pattern to match a literal
    JF> dollar sign:

    JF> grep -P makan\$ file.txt

    you are escaping that from the shell but the 'perl' regex will then make
    it match the end of a line or before a newline. you need to quote it
    against the shell with '' and keep the \ in there.

    uri

    --
    Uri Guttman -- uri AT perlhunter DOT com --- http://www.perlhunter.com --
    ------------ Perl Developer Recruiting and Placement Services -------------
    ----- Perl Code Review, Architecture, Development, Training, Support -------

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedSep 3, '11 at 9:01p
activeSep 4, '11 at 6:35a
posts4
users4
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase