Ubuntu – extract number from a string

bash

i have the following string

Device Enabled (126):   1

can i extract in a variable VAR only 126 and NOT (126) ?

p.s 126 = random number

please help me !

EDIT
i paste my script so u can understand the solution

ID=`xinput list | grep -i TouchPad | awk -F= '{ print $2}' | awk '{print $1}'`
VALOREENABLE=`xinput list-props $ID | grep -i Enabled |awk '{print $3}'`
VALORESENSE=`xinput list-props $ID | grep -i Profile |awk '{print $3}'

in my case VALOREENABLE = (126)
and VALORESENSE (256) i want 126, 256 only without () :(`

Best Answer

echo "Device Enabled (126):   1" | grep -P -o "[0-9]+" | head -1
  • echo puts your string into the pipe
  • that goes to grep, which is a program to apply regular expressions. The option -P enables PEARL like behaviour (which enables you to use the +), and the -o tells grep to only output the matching part of the string. Each match will be printed to a new line
  • use head to pick the line you want. Since we want the first number, we will pick the first line. If you were interested in the one, you would do head -2.
Related Question