grep: repetition-operator operand invalid
Asked Answered
W

3

33

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
Wavelength answered 21/3, 2013 at 2:33 Comment(4)
It appears that on your system grep and ack accept slightly different regular expression syntax. Delete repetition characters from the regex until you find the one that grep doesn't like.Evangelize
My mistake. I edited it to be correct. Same problem.Wavelength
You misunderstood my comment. I'm saying that the grep regex language and the ack regex language are not the same. I'm trying to help you debug the problem yourself to find the difference. (Here's the answer.)Evangelize
jspooner: I'm the author of ack, and the problem is that ack uses Perl regular expressions, because it is a Perl app. grep does not use Perl regular expressions, and so there are differences, as you have found. As to "I don't have ack on this machine", ack is a single file you can download with a single command line at beyondgrep.com/install , no root privs required.Transmigrate
A
35
$ 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

Aboveboard answered 6/8, 2017 at 16:27 Comment(4)
what to do if grep doesnt support -P now?Bridgeman
@Bridgeman what do you mean? It should still support -P.Aboveboard
Installing ggrep via brew saved me, thank you so much !Clipped
To clarify, installing grep via brew doesn't affect existing grep command. It installs everything under ggrep.Tillo
E
29

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

Evangelize answered 3/5, 2013 at 20:11 Comment(1)
I used *, thanks for the solutions, working fine with meBoone
B
1

Try this

grep -Eoh 'heads/.*' text.txt | grep -Eoh '/.*' | grep -Eoh '[a-zA-Z].*'
Beebeebe answered 15/12, 2020 at 4:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.