I’m running on MacOS.
I have a file which contains these lines:
This is ('apple').
This is ('orange').
This is ('banana').
When I search for an apple using sed it works:
cat file.txt | sed -n "/(('apple')/p"
But when I try to search for an apple or an orange, it does not work and returns nothing:
cat file.txt | sed -n "/(('apple'|'orange')/p"
What should be the correct sed regex to use or operator inside a group capture??
Thank you.
0
sed accepts BRE regex patterns by default, where (
, )
and |
have to be preceded by to be used as special characters for grouping and alternation:
sed -n "/(('apple'|'orange'))/p" file.txt
2
I finally make it work by adding the -E flag.
cat file.txt | sed -nE "/('(apple|orange)/p"