How grep with -e (regex) `/log/messages` ? [ solved ]
from Gordon_F@lemmy.ml to linux@lemmy.ml on 01 Oct 2024 05:49
https://lemmy.ml/post/20899808

Hi,

I would like to display the new lines of /var/log/messages that contain either IN_MyText or OUT_MyText (no matter where in the line)

I’ve tried

tail -fn 3 /var/log/messages | grep --color --line-buffered -e "(IN|OUT)_MyText"

But the output stay blank, when it should not…

Any ideas ?

#linux

threaded - newest

vk6flab@lemmy.radio on 01 Oct 2024 05:56 next collapse

Do you get output if you use that exact tail command without the grep pipe?

Gordon_F@lemmy.ml on 01 Oct 2024 06:18 collapse

Yes

thingsiplay@beehaw.org on 01 Oct 2024 06:08 next collapse

grep by default uses Basic Regular Expressions. This means the ( and ) lose their special meaning and are matched literally. Either use a backslash version \( to have a group, or use Extended Regular Expressions with -E “(IN|OUT)” . In man grep under REGULAR EXPRESSIONS are some differences noted.

Gordon_F@lemmy.ml on 01 Oct 2024 06:20 collapse

Thank you ! @thingsiplay@beehaw.org 👍
-E solved it :)

CosmicGiraffe@lemmy.world on 01 Oct 2024 17:45 collapse

It’s marked solved, but since OP didn’t post the solution:

-e uses basic regular expressions, where you need to escape the meta-characters ((|)) with a backslash. Alternatively, use extended regex with -E

$ echo a | grep -E "(a|b)"
a
$ echo a | grep -e "\(a\|b\)"
a
$ echo a | grep -e "(a|b)"
$ echo a | grep -E "\(a\|b\)"