Link to home
Start Free TrialLog in
Avatar of fixitben
fixitben

asked on

Bash Search for string then get the following numbers

I have some parameters that the user can pass to the command, but I need to get the number following a specific string.

Here are some examples of the parameters.

xxx   cpus="12" memory="1500mb"
xxx  -cpus 12 -memory 1500mb
xxx  cpus=12 memory=1500mb

I would like to search for the first occurrence of cpus and return the number 12.
The number could be any where from 1 to 1000.

Thanks
Fixitben
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of fixitben
fixitben

ASKER

Thanks for your help.
This worked well.
bash -c 'a=$*;a=${a#*cpus[= ]};echo ${a%% *}'  xxx   cpus="12" memory="1500mb"
bash -c ''a=$*;a=${a#*cpus[= ]};echo ${a%% *}'  xxx   -cpus 12 -memory 1500mb
bash -c 'a=$*;a=${a#*cpus[= ]};echo ${a%% *}' xxx  cpus=12 memory=1500mb