views:

32

answers:

3

I try to get all occurences of a pattern from a file, but currently I fail if there is more than one occurence per line.

Sample line in file:

lorem ipsum foo="match1" lorem ipsum foo="match2" lorem ipsum

The output I want:

match1 match2

I tried getting this using sed:

sed -ne 's/^.*foo="\([^"]*\)".*$/\1/p'

With this expression I only get the first occurence, but I don't know how to make it better.

Thanks for your help.

+2  A: 

Use grep instead of sed.

grep -o -P '(?<=foo=")[^"]*'
KennyTM
Be aware that this only works if your `grep` happens to have been compiled with support for the `-P` (Perl syntax) option. Vanilla `grep` doesn't support lookbehind or any of the other whizbang features found in Perl and the Perl-derived regex flavors.
Alan Moore
A: 
$ s="lorem ipsum foo="match1" lorem ipsum foo="match2" lorem ipsum"
$ echo $s|tr "[ \t]" "\n"|awk -F"=" '$1=="foo"{print $2}'
match1
match2
ghostdog74
A: 

This is to show you that you probably don't want to use sed. It works and it's fairly robust, but it may fail on some corner cases:

sed -n 's/\(foo="[^"]*\)/\n\1\n/g;s/[^\n]*\n*foo="//g;s/\n"[^n]*$//p' file
Dennis Williamson