Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

For example, I want to get only the 3rd element in each row when I call:

xinput --list --short|grep "slave  pointer"

I get the output:

⎜   ↳ Virtual core XTEST pointer                id=4    [slave  pointer  (2)]
⎜   ↳ SynPS/2 Synaptics TouchPad                id=11   [slave  pointer  (2)]
⎜   ↳ MCE IR Keyboard/Mouse (ite-cir)           id=12   [slave  pointer  (2)]
⎜   ↳ Logitech Unifying Device. Wireless PID:101a   id=14   [slave  pointer  (2)]

I would like to get only the names like "SynPS/2 Synaptics TouchPad", ...

I saw somewhere a solution with awk and print somehow, but isn't there a simpler solution to achieve that without awk or perl or such?

share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

Gawk is pretty simple for this kind of thing but OK, you can also use cut:

xinput --list --short|grep "slave  pointer" | cut -f 1

That will also include the leading space and characters. If you need to get rid of those, try this:

xinput --list --short|grep "slave  pointer" | cut -f 1 | cut -d" " -f 5-
share|improve this answer
    
THANKS, cut is what i was looking for –  rubo77 Dec 3 '12 at 21:35
    
to get rid of the spaces at the end i used xinput --list --short|grep "slave pointer" | cut -f 1| cut -d" " -f 5-|sed 's/\s\+$//g' –  rubo77 Dec 4 '12 at 0:15
    
How do I use cut with the output of ps? see unix.stackexchange.com/questions/79264/… –  rubo77 Jun 13 '13 at 9:32
    
@rubo77 You don't really, you either set the ps output format or use awk as the answers to that question suggest. –  terdon Jun 13 '13 at 12:16
add comment

If it were the third element, that would be "SynPS/2", not "SynPS/2 Synaptics TouchPad"

The best you can achieve here is report what's before id= as it's the only thing that can identify where the device name ends.

xinput list | sed -n 's/^[ ⎜↳]*\(.*[^[:blank:]]\)[[:blank:]]*id=.*slave  pointer.*/\1/p'

Or with GNU grep built with PCRE support (-P option):

xinput list | grep -Po '[^ ⎜↳].*?(?=\s+id=.*slave  pointer)'
share|improve this answer
    
The fields are separated by tabs so default cut will work. –  terdon Dec 3 '12 at 16:31
    
OK, I can see when editing the question that they're there, but not in the rendered version. I've edited my answer so that it works in either case. –  Stéphane Chazelas Dec 3 '12 at 16:59
add comment

Your Answer

 
discard

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.