End of line char ($) doesn't work inside square brackets

前端 未结 3 1542
一向
一向 2021-01-12 21:29

Putting $ inside square brackets doesn\'t work for grep.

~ $ echo -e \"hello\\nthere\" > example.txt
~ $ grep \"hello$\" example.txt 
hello
~         


        
3条回答
  •  感动是毒
    2021-01-12 22:09

    You can, however, use $ in a parenthesis grouping, which facilitates the use of | (or), which can accomplish the same idea as a square bracket group.

    Something like the following might be of interest to you:

    ~ $ cat example.txt 
    hello
    there
    helloa
    hellob
    helloc
    
    ~ $ grep "hello\($\|[ab]\)" example.txt
    hello
    helloa
    hellob
    

提交回复
热议问题