Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 () :(`

share|improve this question
up vote 2 down vote accepted

There are lots of ways to do it. Here's the one that first comes to mind for me:

VALOREENABLE=`xinput list-props $ID | grep -i Enabled | grep -o "(.*)" | tr -d '()'`
VALORESENSE=`xinput list-props $ID | grep -i Profile | grep -o "(.*)" | tr -d '()'`
share|improve this 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.
share|improve this answer
sho my edit post :P – r1si Jan 8 '12 at 22:38

One way using sed:

sed 's/.*(\([^)]*\)).*/\1/' <<<"Device Enabled (126):   1"



so for your script you could try:


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}'`
VALORESENSE=`xinput list-props $ID | grep -i Profile | awk '{print $4}' | sed 's/.*(\([^)]*\)).*/\1/' `
share|improve this answer
show my edit post – r1si Jan 8 '12 at 22:38

OK, I will toss in my 2c, you can do it directly with awk

ID=`xinput list | grep -i TouchPad | awk -F= '{ print $2}' | awk '{print $1}'`
VALOREENABLE=`xinput list-props $ID | awk '/Enabled/ {print substr ($3, 2, 3) }'`
VALORESENSE=`xinput list-props $ID | awk '/Profile/ {print substr ($3, 2, 3) }'
share|improve this answer

The shell can do it:

str="Device Enabled (126):   1"

Or, if you really like using awk, set the field separator to be either parenthesis

number=$(awk -F '[()]' '{print $2}' <<< "Device Enabled (126):   1")

Using your example code, I'd write:

ID=$(xinput list | awk -F= '/TouchPad/ {split($2, a, /[[:space:]]\+/); print a[1]}')
PROPS=$(xinput list-props $ID)
VALOREENABLE=$(awk -F '[()]' '/Enabled/ '{print $2}' <<< "$PROPS")
VALORESENSE=$(awk -F '[()]' '/Profile/ '{print $2}' <<< "$PROPS")
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.