The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Operating Systems > Linux
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #2 (permalink)  
Old 05-29-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Regular expressions, as such, only "match", they don't "extract". Some scripting languages have a facility for returning the part of a regular expression which matched, but it then depends on which language you want.

Without more information about what to look for precisely, the simple answer is that the regular expression "y" will match the letter "y", and the matching (extracted) string will always be "y".

If you want the first substring between two periods, that's something like this:


Code:
sed -n 's/.*\.\([^.]*\)\..*/\1/p' file

or with Perl:


Code:
perl -lne 'if (m/\.([^.]*)\./) { print $1 }' file

or with awk:


Code:
awk -F . '{ print $2 }' file

The latter doesn't use regular expressions at all, though.

But really, you need to explain in more detail what the parameters of the problem are.

Last edited by era; 05-29-2008 at 04:30 AM.. Reason: Add Perl example