I know that there's ^ and $ but those are for the beginning and end of line, I am trying to match the beginning and end of string/word.

I tried to use \b, which didn't work. For example:

echo "lol llol" | grep -E \blol
  • 2
    You probably need to protect the \ from interpretation by your shell - either '\blol' or \\blol – steeldriver Apr 25 '16 at 12:40

You can use either of these, depending on what you're trying to display:

$ echo "lol llol" | grep -E "\blol"
lol llol

$ echo "lol llol" | grep -Eo "\blol"

Putting the regex in quotes solves your matching problem. The -o flag causes grep to only print the matched string instead of the entire line.

  • I thought about my grep command being wrong, but couldn't figure out I should have put them in quotes, that usually defeats the purpose of backslash. – DisplayName Apr 25 '16 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.