tags:

views:

531

answers:

5

I’m stuck in trying to grep anything just after name=, include only spaces and alphanumeric.

e.g.:

name=some value here

I get

some value here

I’m totally newb in this, the following grep match everything including the name=.

grep 'name=.*' filename

Any help is much appreciated.

+1  A: 

grep will print the entire line where it matches the pattern. To print only the pattern matched, use the grep -o option. You'll probably also need to use sed to remove the name= part of the pattern.

grep -o 'name=[0-9a-zA-Z ]'  myfile | sed /^name=/d
Charles Ma
Two processes for this is excessive. If you have to use sed, may as well do it with that alone.
John Zwinck
+2  A: 

grep does not extract like you expect. What you need is

grep "name=" file.txt | cut -d'=' -f1-

Stefano Borini
+4  A: 

As detailed here, you want a positive lookbehind clause, such as:

grep -P '(?<=name=)[ A-Za-z0-9]*' filename

The -P makes grep use the Perl dialect, otherwise you'd probably need to escape the parentheses. You can also, as noted elsewhere, append the -o parameter to print out only what is matched. The part in brackets specifies that you want alphanumerics and spaces.

The advantage of using a positive lookbehind clause is that the "name=" text is not part of the match. If grep highlights matched text, it will only highlight the alphanumeric (and spaces) part. The -o parameter will also not display the "name=" part. And, if you transition this to another program like sed that might capture the text and do something with it, you won't be capturing the "name=" part, although you can also do that by using capturing parenthess.

Mark Santesson
does grep actually support positive lookbehind?
Charles Ma
I was worried about that, too, so I tried it on my Windows PC, and that is why I had to use the -P parameter. It probably supports it in the other flavors (maybe not -G), but I didn't know the proper escaping for the elements. I'm using grep 2.5.1.
Mark Santesson
I get this with grep -P: grep: Support for the -P option is not compiled into thisThis is on Ubuntu Hardy (Linux 2.6.24). On another Linux machine it failed with a slightly different error message, and likewise on Solaris 10.
John Zwinck
Yep, read up a bit on this, it's an experimental option that a lot of operating systems don't include by default yet. As for highlighting machine text, grep --color does that too :)
Charles Ma
I thought that I might need to use `sed` to get what I need. I know the -P is not compiled in some of the machine out there, but anyway its in my mach and it does what I need. Really thanks a lot for this. Never thought of positive lookbehind.
Kwokfu
+4  A: 

Try this:

sed -n 's/^name=//p' filename

It tells sed to print nothing (-n) by default, substitute your prefix with nothing, and print if the substitution occurs.

Bonus: if you really need it to only match entries with only spaces and alphanumerics, you can do that too:

sed -n 's/^name=\([ 0-9a-zA-Z]*$\)/\1/p' filename

Here we've added a pattern to match spaces and alphanumerics only until the end of the line ($), and if we match we substitute the group in parentheses and print.

John Zwinck
+1. Simple, elegant, performant.
Sinan Ünür
Most likely a very suitable solution, but the asker asked about grep... and it doesn't match only spaces and alphanumerics.
Mark Santesson
Matching only spaces and alphas is trivial, I'll add something for that. As for only using grep, well, the top-ranked answer right now uses grep AND sed...at least this only uses a single process. :)
John Zwinck
I love your answer too, as I’m also messing with `sed` now. Thanks for your tips!
Kwokfu
+2  A: 

gawk

echo "name=some value here" | awk -F"=" '/name=/ { print $2}'

or with bash

str="name=some value here"
IFS="="
set -- $str
echo $1    
unset IFS

or

str="name=some value here"
str=${str/name=/}
ghostdog74