3

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

6 Answers 6

2

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 '()'`
2
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.
1
  • sho my edit post :P
    – r1si
    Jan 8, 2012 at 22:38
2

One way using sed:

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

Result:

126

so for your script you could try:

#!/bin/bash

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/' `
echo $VALORESENSE
1
  • show my edit post
    – r1si
    Jan 8, 2012 at 22:38
2

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) }'
1

The shell can do it:

str="Device Enabled (126):   1"
old_IFS=$IFS
IFS="()"
words=($str)
number=${words[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")
0

Extracting the ID could be simplified as so (note the use of $() instead of backticks):

ID=$(xinput list | awk '/TouchPad/{sub(/^.*id=/,"");sub(/\[.*/,"");print}')

Personally, I'd use perl in extracting the numbers:

VALOREENABLE=$(xinput list-props $ID | perl -ne 's/.*\((.*)\).*/$1/ and print if /Enabled/' )
VALORESENSE=$(xinput list-props $ID | perl -ne 's/.*\((.*)\).*/$1/ and print if /Profile/' )

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .