To all the 'sed' doctors out there:
I have a seemingly trivial 'sed' question to which I have not been able to find a solution.
How can you get 'sed' to exctract a regular expression it has matched in a line?
In other words words, I want just the string corresponding to the regular expression with all the non-matching characters from the containing line stripped away.
I tried using the back-reference feature like below
regular expression to be isolated
gets `inserted`
here
|
v
sed -n 's/.*\( \).*/\1/p
this works for some expressions like
sed -n 's/.*\(CONFIG_[a-zA-Z0-9_]*\).*/\1/p
which neatly extracts all macro names starting with 'CONFIG_ ....' ( found in some '*.h' file ) and prints them all out line by line
CONFIG_AT91_GPIO
CONFIG_DRIVER_AT91EMAC
.
.
CONFIG_USB_ATMEL
CONFIG_USB_OHCI_NEW
.
e.t.c.
BUT the above breaks down for something like
sed -n 's/.*\([0-9][0-9]*\).*/\1/p
this always returns single digits like
7
9
.
.
6
rather than extracting a contigious number field such as.
8908078
89670890
.
.
.
23019
.
e.t.c.
P.S.: I would be grateful to feedback on how this is achieved in 'sed'.
I know how to do this with 'grep' and 'awk'
I would like to find out if my - albeit limited - understanding of
'sed' has holes in it and if there is way to do this in 'sed' which I
have simply overlooked.