1

Suppose I have a file that includes the line:

file: "print.exe", name: "David Albertas", address: "New York City"

I want to use grep and regexp that I get the output -

name: "David Albertas"

and only this output!

I wrote the command:

 egrep -o "name: \".*\"" script

but I get bigger output than what I expected which is:

name: "David Albertas", address: "New York City"

This is not good enough. Can you think of a regexp that can help me? For your convenience, the file is attached.

CrazySynthax
  • 13,662
  • 34
  • 99
  • 183

2 Answers2

2

You need a lazy quantifier. Use grep with P flag for --perl-regexp.

grep -oP "name: \".*?\"" script
Community
  • 1
  • 1
bobble bubble
  • 16,888
  • 3
  • 27
  • 46
  • thanks. What if I want to get only "David Albertas" without the "name" prefix ? Do you have a regexp for this? – CrazySynthax Feb 29 '16 at 09:47
  • 1
    @CrazySynthax You can use a [lookbehind](http://www.regular-expressions.info/lookaround.html): `grep -oP "(?<=name: )\".*?\"" script` – bobble bubble Feb 29 '16 at 09:50
  • 1
    @CrazySynthax Or without the double quotes: `grep -oP "(?<=name: \")[^\"]+"` – bobble bubble Feb 29 '16 at 09:53
  • 2
    or since you use `grep -P` you could use the `\K` regex escape sequence : `grep -oP "name: \K\".*?\"" script` will return only the "name" part – Aaron Feb 29 '16 at 09:53
2

If you are sure (as in your pattern) the enclosing quotes you might use the negation of the quotes technique:

egrep -o 'name: "[^"]*"' INPUT

If you want only the name (after name:) you might want to use somehting like sed:

sed 's/.*name: \("[^"]\+"\).*/\1/' INPUT

Or you can use look-around regexps with grep.

grep -oP '(?<=name: )".*?"' INPUT
Zsolt Botykai
  • 50,406
  • 14
  • 85
  • 110