即使bash中的列数可以更改,也可以将其提取到bash中 [英] Extract a column in bash even if the number of columns before it can change
问题描述
im试图使此脚本在wacom平板电脑上切换区域,im使用xsetwacom配置平板电脑,
im trying to make this script to switch areas on wacom tablets, im using xsetwacom to configure the tablets,
这是脚本试图显示外观的方式(它可以工作,但仅适用于第一台平板电脑)
this is how the script im trying to make looks (it works, but just with the first tablet)
#!/bin/bash
variable =`xsetwacom --list | awk '/stylus/ {print $7}'`
xsetwacom --set $variable area 123 123 123 123
这是xsetwacom --list输出的外观
this is how an output of xsetwacom --list looks
Wacom Intuos S Pad pad id: 21 type: PAD
Wacom Intuos S Pen stylus id: 22 type: STYLUS
Wacom Intuos S Pen eraser id: 23 type: ERASER
并连接了其他平板电脑
Wacom Bamboo 2FG 4x5 Pad pad id: 21 type: PAD
Wacom Bamboo 2FG 4x5 Ped stylus id: 22 type: STYLUS
Wacom Bamboo 2FG 4x5 Pen eraser id: 23 type: ERASER
Wacom Bamboo 2FG 4x5 Finger touch id: 24 type: TOUCH
因此,当我放置另一台平板电脑时,得到的"$ variable"值会发生变化,因为有更多的单词,我该如何解决,我正在寻找的值是手写笔的ID号,谢谢!./p>
So when i put another tablet, the value of the "$variable" that i get changes, because theres more words, how can i fix this, the value im looking for is the id number of stylus,Thanks!.
推荐答案
Bash具有内置的正则表达式支持,可以如下使用:
Bash has built-in regex support, which can be used as follows:
id_re='id:[[:space:]]*([[:digit:]]+)' # assign regex to variable
while IFS= read -r line; do
[[ $line = *stylus* ]] || continue # skip lines without "stylus"
[[ $line =~ $id_re ]] || continue # match against regex, or skip the line otherwise
stylus_id=${BASH_REMATCH[1]} # take the match group from the regex
xsetwacom --set "$stylus_id" area 123 123 123 123 </dev/null
done < <(xsetwacom --list)
在 https://ideone.com/amv9O1 上,您可以看到此程序正在运行(输入来自stdin而不是xsetwacom --list
),并为这两行设置stylus_id
.
At https://ideone.com/amv9O1 you can see this running (with input coming from stdin rather than xsetwacom --list
, of course), and setting stylus_id
for both of your lines.
这篇关于即使bash中的列数可以更改,也可以将其提取到bash中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!