Perl + one-liner smart Perl command, in place grep to match unusual characters + full match

前端 未结 3 1120
隐瞒了意图╮
隐瞒了意图╮ 2021-01-28 00:14

Subject: one-liner smart Perl command instead of a simple grep (in Bash script)

I have a problem when I use grep to match the unusual characters (I can\'t put the \"\\\"

相关标签:
3条回答
  • 2021-01-28 00:57

    I'm not sure I understand your question, exactly. Perhaps -P and -x do what you want, along with quoting "$Some_string" to preserve whitespace?

    grep -Px "$Some_string" file
    

    From the grep man page:

    -P, --perl-regexp

    Interpret PATTERN as a Perl regular expression.

    -x, --line-regexp

    Select only those matches that exactly match the whole line.

    0 讨论(0)
  • 2021-01-28 01:01

    You could do something like this:

    perl -ne 'print if m|\Q[ * ( & % \E\@\Q !]\E|' file
    

    I've found the \Q and \E operator in Quote and Quote-like Operators.

    A snippet from that link:

    You cannot include a literal $ or @ within a \Q sequence. An unescaped $ or @ interpolates the corresponding variable, while escaping will cause the literal string \$ to be inserted. You'll need to write something like m/\Quser\E@\Qhost/.

    \Q  Quote (disable) pattern metacharacters till \E or
        end of string
    
    \E  End either case modification or quoted section
        (whichever was last seen)
    
    0 讨论(0)
  • 2021-01-28 01:07

    You will have to escape the string somehow. Using \ to escape special characters is required in this case, because passing the string to Perl is really no different than passing it to grep -- both will see the string after the shell processes it.

    Using Perl to do this will simply obfuscate your code.

    0 讨论(0)
提交回复
热议问题