matching a line with a literal asterisk “*” in grep

后端 未结 5 1107
迷失自我
迷失自我 2020-12-15 17:42

Tried

$ echo \"$STRING\" | egrep \"(\\*)\"

and also

$ echo \"$STRING\" | egrep \'(\\*)\'

and countless ot

相关标签:
5条回答
  • 2020-12-15 18:02

    Simply escape the asterisk with a backslash:

    grep "\*"
    
    0 讨论(0)
  • 2020-12-15 18:07
    echo "$STRING" | fgrep '*'
    

    fgrep is used to match the special characters.

    0 讨论(0)
  • 2020-12-15 18:14

    Try a character class instead

    echo "$STRING" | egrep '[*]' 
    
    0 讨论(0)
  • 2020-12-15 18:26

    Use:

    grep "*" file.txt
    

    or

    cat file.txt | grep "*"
    
    0 讨论(0)
  • 2020-12-15 18:28

    Here's one way to match a literal asterisk:

    $ echo "*" | grep "[*]"
    *
    $ echo "*" | egrep "[*]"
    *
    $ echo "asfd" | egrep "[*]"
    $ echo "asfd" | grep "[*]"
    $ 
    

    Wrapping an expression in brackets usually allows you to capture a single special character easily; this will also work for a right bracket or a hyphen, for instance.

    Be careful when this isn't in a bracket grouping:

    $ echo "hi" | egrep "*"
    hi
    $ echo "hi" | grep "*"
    $
    
    0 讨论(0)
提交回复
热议问题