Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sed help 3

Status
Not open for further replies.

aswolff

Programmer
Jul 31, 2006
100
US
I am on Solaris 9. I am trying to use sed for a simple regexp.

I have the text "123 Juice". I would like sed to just display the "123".

My command:

Code:
echo "123 Juice" | sed "s/(\d\d\d)\s\w+/\1/"

returns

sed: Command Garbled




Thanks.
 
Err... sed "s/123 Juice/123/"




In order to understand recursion, you must first understand recursion.
 
I guess I was looking for a more general form so that I can tackle a more challenging problem:

Given the output of the unix command 'id'
display the contents within the first set of parentheses

uid=80(userid) gid=80(groupid)

return: userid

Thanks.
 
why sed?

cut -d' ' -f1

or

awk '{print $1}'


HTH,

p5wizard
 
Why does'nt this return what's inside the first set of parenthese?

id | awk '{print $1}' | sed -e "s/(\(.+\))/$1/
 
Hmmm...that returns an error:

awk: syntax error near line 1
awk: bailing out near line 1

I tried the below:
Code:
id | awk '{print $1}' | awk -F'(' '{print $2}'

and it returned:
Code:
userid)

I I could just get rid of the trailing ) I would be all set.
 
I finally get my desired result but it's ugly. I am looking for a more elegant solution (Slow day at work!)

Code:
id | awk '{print $1}' | awk -F'(' '{print $2}' | sed -e "s/)//g"

There has got to be a nicer looking solution?
 
try nawk instead:
Code:
id | nawk -F '[()]' '{print $2}'

or if that doesn't do it

Code:
id | tr '()' '::' | awk -F':' '{print $2}'


HTH,

p5wizard
 
Code:
id | nawk -F '[()]' '{print $2}'

worked as well. Thanks p5
 
Back with your original question - I don't think that sed understands the \d, \s or \w so
Code:
echo "123 Juice" | sed 's/([0-9]+) [A-z]+/\1/'
will do what you want.

Having said that the Lemur book allows for the Posix classes [:alnum:], [:alpha:], [:digit:] and [:space:] (amoungst others) so
Code:
echo "123 Juice" | sed 's/([:digit:]+)[:space:]+[:alnum:]+/\1/'
is what you were trying to write.

Ceci n'est pas une signature
Columb Healy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top