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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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
ASKER
This worked well.