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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using sed inside bash to modify variables 2

Status
Not open for further replies.

forrie

MIS
Mar 6, 2009
91
0
0
US
I have a perplexing sed question.

I want to parse the output of a script. The output is from the result of /sbin/service mysql status, which is rougly (in one case):

MySQL Running (12345)

I want to assign the PID to a variable, so that means piping to sed a couple of times and removing the .*( and ending ).* to strip it out. Easier said than done, as it turns out the stock Linux sed doesn't like using a wildcard expression such as: sed 's/.*\(//'

So I wonder if there is a more clever way to do this. Sure, I know where the PID file is, but the goal of this other script is to utilize the info that the redhat scripts already have access to (not reinvent the wheel).


Thanks.
 
What about this ?
Code:
sed -n '/MySQL/{s!.*(!!;s!).*!!;p;}'

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
and what about bash built-ins?

Code:
$ pid="MySQL Running (12345)"
$ echo $pid
MySQL Running (12345)
$ pid=${pid#*(}
$ echo $pid
12345)
$ pid=${pid%)}
$ echo $pid
12345



HTH,

p5wizard
 
Thanks, PHV, that worked for me. It's more esoteric than I'm used to (shows I got a bit to learn on regex). Could you walk through what that's doing?

Re: the shell builtins, I got mixed results when trying to use that approach from within the script.

Thanks both of you for your advice and help, I appreciate it!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top