正则表达式不是很好,更不用说grep里面的正则表达式,但我接近我需要的东西。我有这个鼠标,我想提取它的ID(它有2个ID作为指针打印,一个作为键盘打印)我不能使grep只打印指针值的id号。
输入文字如下:
⎡ Virtual core pointer id=2 [master pointer (3)]
⎜ ↳ Virtual core XTEST pointer id=4 [slave pointer (2)]
⎜ ↳ ELAN Touchscreen id=12 [slave pointer (2)]
⎜ ↳ DLL06E4:01 06CB:7A13 Touchpad id=13 [slave pointer (2)]
⎜ ↳ Logitech Gaming Keyboard G910 id=17 [slave pointer (2)]
⎜ ↳ Razer Razer DeathAdder Chroma id=20 [slave pointer (2)]
⎜ ↳ Razer Razer DeathAdder Chroma id=21 [slave pointer (2)]
⎜ ↳ Logitech Logitech G633 Gaming Headset id=22 [slave pointer (2)]
⎣ Virtual core keyboard id=3 [master keyboard (2)]
↳ Virtual core XTEST keyboard id=5 [slave keyboard (3)]
↳ Power Button id=6 [slave keyboard (3)]
↳ Video Bus id=7 [slave keyboard (3)]
↳ Video Bus id=8 [slave keyboard (3)]
↳ Power Button id=9 [slave keyboard (3)]
↳ Sleep Button id=10 [slave keyboard (3)]
↳ Integrated_Webcam_HD id=11 [slave keyboard (3)]
↳ AT Translated Set 2 keyboard id=14 [slave keyboard (3)]
↳ Dell WMI hotkeys id=16 [slave keyboard (3)]
↳ Logitech Gaming Keyboard G910 id=18 [slave keyboard (3)]
↳ Razer Razer DeathAdder Chroma id=19 [slave keyboard (3)]
我接近的方式是使用这个命令:
xinput --list --short | grep -oP 'Razer.*id=\K([0-9]+)(.*?)pointer'
但打印出来:
20 [slave pointer
21 [slave pointer
如何才能打印20
和21
?需要pointer
,因为我只需要指针ID。
答案 0 :(得分:2)
您可以在正则表达式中使用前瞻来在pointer
个数字之后声明id
的存在而不实际匹配它:
xinput --list --short | grep -oP 'Razer.*id=\K(\d+)(?=.*pointer)' file
<强>输出:强>
20
21