Grep仅输出正则表达式匹配

时间:2022-01-25 14:02:24

Not very good with regex let alone regex inside grep but I'm close to what I need. I have this mouse that I want to extract it's ids (it has 2 ids printed as pointer and one as keyboard) and I can't make grep to only print the id numbers for pointer values.

正则表达式不是很好,更不用说grep里面的正则表达式,但我接近我需要的东西。我有这个鼠标,我想提取它的ID(它有2个ID作为指针打印,一个作为键盘)我不能使grep只打印指针值的id号。

The input text is as follows:

输入文本如下:

⎡ 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)]

⎡虚拟核心指针id = 2 [主指针(3)]⎜⎜虚拟核心XTEST指针id = 4 [从指针(2)]⎜⎜ELAN触摸屏id = 12 [从指针(2)]⎜⎜DLL06E4:01 06CB :7A13触控板id = 13 [从指针(2)]⎜⎜罗技游戏键盘G910 id = 17 [从指针(2)]⎜⎜RazerRazer DeathAdder色度id = 20 [从指针(2)]⎜⎜RazerRazer DeathAdder色度id = 21 [从指针(2)]⎜⎜罗技Logitech G633游戏耳机id = 22 [从指针(2)]⎣虚拟核心键盘id = 3 [主键盘(2)]↳虚拟核心XTEST键盘id = 5 [从属键盘(3)]↳电源按钮id = 6 [从属键盘(3)]↳视频总线id = 7 [从属键盘(3)]↳视频总线id = 8 [从属键盘(3)]↳电源按钮id = 9 [从属键盘(3)]↳睡眠按钮id = 10 [从属键盘(3)]↳Inte​​grated_Webcam_HDid = 11 [从属键盘(3)]↳在转换后设置2键盘id = 14 [从属键盘(3)] ↳DellWMI热键id = 16 [从属键盘(3)]↳罗技游戏键盘G910 id = 18 [奴隶键盘(3)]↳RazerRazer DeathAdder色度id = 19 [奴隶键盘(3)]

The way I got close is using this command:

我接近的方式是使用此命令:

xinput --list --short | grep -oP 'Razer.*id=\K([0-9]+)(.*?)pointer'

xinput --list --short | grep -oP'Razer。* id = \ K([0-9] +)(。*?)指针'

but this prints:

但这打印:

20 [slave pointer 21 [slave pointer

20 [从指针21 [从指针

How can I make it only print 20 and 21? pointer is needed because I only want the ids of pointers.

如何才能使它只打印20和21?指针是必需的,因为我只想要指针的id。

1 个解决方案

#1


2  

You can use lookahead in your regex to assert presence of pointer after your id numbers without actually matching it:

你可以在你的正则表达式中使用lookahead在你的id号码之后断言指针的存在而不实际匹配它:

xinput --list --short | grep -oP 'Razer.*id=\K(\d+)(?=.*pointer)' file

Output:

20
21

#1


2  

You can use lookahead in your regex to assert presence of pointer after your id numbers without actually matching it:

你可以在你的正则表达式中使用lookahead在你的id号码之后断言指针的存在而不实际匹配它:

xinput --list --short | grep -oP 'Razer.*id=\K(\d+)(?=.*pointer)' file

Output:

20
21