Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grep: repetition-operator operand invalid

Tags:

grep

ack

I have this regular express (?<=heads\/)(.*?)(?=\n) and you can see it working here http://regexr.com?347dm

I need this regex to work in the grep command but I'm getting this error.

$ grep -Eio '(?<=heads\/)(.*?)(?=\n)' text.txt 
grep: repetition-operator operand invalid

It works great in ack but I dont have ack on the machine I need to run this on.

ack text.txt -o --match '(?<=heads\/)(.*?)(?=\n)'

text.txt

74f3649af36984e1b784e46502fe318e91d29570    HEAD
06d4463ab47a6246e6bd94dc3b9267d59fc16c2e    refs/heads/ARC
0597e13c22b6397a1b260951f9d064f668b26f08    refs/heads/LocationAge
e7e1ed942d15efb387c878b9d0335b37560c8807    refs/heads/feature/311-312-breaking-banner-updates
d0b2632b465702d840a358d0b192198ae505011c    refs/heads/gulf-news
509173eafc6792739787787de0d23b0c804d4593    refs/heads/jbb-new-applicationdidfinishlaunching
1e7b03ce75b1a7ba47ff4fb5128bc0bf43a7393b    refs/heads/locationdebug
74f3649af36984e1b784e46502fe318e91d29570    refs/heads/master
5d2ede384325877c24db7ba1ba0338dc7b7f84fb    refs/heads/mixed-media
3f3b6a81dd3baea8744aec6b95c2fe4aaeb20ea3    refs/heads/post-onezero
4198a43aab2dfe72d7ae9e9e53fbb401fc9dac1f    refs/heads/whitelabel
76741013b3b2200de29f53800d51dfd6dc7bac5e    refs/tags/r10
fc53b1a05dad3072614fb397a228819a67615b82    refs/tags/r10^{}
afdcfd970c9387f6fda0390ef781c2776aa666c3    refs/tags/r11
like image 727
jspooner Avatar asked Mar 21 '13 02:03

jspooner


2 Answers

grep does not support the (?<=...) or *? or (?=...) operators. See this table.

like image 129
Raymond Chen Avatar answered Sep 25 '22 06:09

Raymond Chen


$ grep -Pio '(?<=heads\/)(.*?)(?=\n)' text.txt # P option instead of E

If you use GNU grep, you can use -P or --perl-regexp options.

In case you are using OS X, you need to install GNU grep.

$ brew install grep 

Due to recent changes, to use GNU grep on macOS you either have to prepend the command with a 'g'

$ ggrep -Pio '(?<=heads\/)(.*?)(?=\n)' text.txt # P option instead of E

Or change the path name

like image 38
Sanghyun Lee Avatar answered Sep 24 '22 06:09

Sanghyun Lee