I was hoping someone could answer my quick question as I am going nuts!
I have recently started learning regular expressions in my Java programming however am a little confused how to get certain features to work correctly directly in BASH. For example, the following code is not working as I think it should.
echo 2222 | grep '2\{2\}'
I am expecting it to return:
22
I have tried variations of it including:
echo 2222 | grep '2{2}'
echo 2222 | grep -P '2\{2\}'
echo 2222 | grep -E '2\{2\}'
However I am completely out of ideas. I’m sure this is a simple parameter / syntax fix and would love some help! P.S I’ve done tons of googling and every reference I find does not work in BASH; regex’s can run on so many different platforms and engines =/
The regex will pattern match on the line, and either print out the whole line (2222) if it matches, or nothing if it doesn’t.
It will NOT pull out a portion of the output. For that, you want something like sed: