0

I'm using grep to extract parts from a file but I'm having trouble making it work properly. From a string,

something0( something1

I want to extract something0. This works fine on some input,

echo 'a b( c d' | grep -Po '(?m)^.+?(?=\(.+)'   #outputs 'a b'

But not so well on other input,

echo 'a b( )c d( e f' | grep -Po '(?m)^.+?(?=\(.+)'   #outputs 'a b' and '( ) c d'

How would I make grep only return the first match or improve my regex? Piping greps output to,

head -n 1

is not an alternative since grep will read an entire file with these lines and if I'm not misstaking this will only output the first ever match in the file. It's okey to assume that something0 doesn't contain any '('.

Thanks.

-P

3 Answers3

0

Er, are you sure?

$ echo 'a b( )c d( e f' | grep -Po '(?m)^.+?(?=\(.+)'
a b

(Also note that the last + is unnecessary; the regex is equivalent to the shorter (?m)^.+?(?=\(.).

Amber
  • 507,862
  • 82
  • 626
  • 550
  • Yes, I'm sure. The output is 'a b' and '( ) c d'. Thank you for pointing out the superfluous '+'. –  Mar 10 '12 at 09:39
0

Try with next regex. Also works:

$ echo 'a b( c d' | grep -Po '^([^(]*)'
a b
$ echo 'a b( )c d( e f' | grep -Po '^([^(]*)'
a b
Birei
  • 35,723
  • 2
  • 77
  • 82
0

What about matching exactly what you want?

^[^(]*?(?=\()
FailedDev
  • 26,680
  • 9
  • 53
  • 73
  • Thank you. I understand the question was trivial but regex is pretty new to me. –  Mar 10 '12 at 09:40