2

我有一个隐藏 X11 窗口的 bash 脚本。我希望能够找到鼠标所在的窗口并取消映射该窗口。

使用xdotool我已经找到了一种查找窗口 ID 的方法:

$ xdotool getmouselocation
x:392 y:344 screen:0 window:54799020

我想将这条线修剪为54799020.
(我想删除所有内容,包括window:.)

有没有办法做到这一点?tr我对和的经验很少sed。我以前习惯sed删除文本,但我还需要删除鼠标坐标,这并不总是相同的。

4

3 回答 3

2

对于您的问题标题中的一般情况,这可以通过至少两种方式单独在 bash 中完成。

一个使用bash 字符串操作

# ${VARIABLE##pattern} trims the longest match from the start of the variable.
# This assumes that "window:nnnnnn" is the last property returned.

DOTOOL_OUTPUT=$(xdotool getmouselocation)
WINDOW_HANDLE=${DOTOOL_OUTPUT##*window:}

作为助记符,#位于键盘左侧$并修剪字符串的开头;%位于字符串的右侧$并修剪字符串的末尾。#%修剪最短的模式匹配;##%%修剪最长。

另一种方式使用bash 正则表达式匹配

# Within bash's [[ ]] construct, which is a built-in replacement for
# test and [ ], you can use =~ to match regular expressions. Their
# matching groups will be listed in the BASH_REMATCH array.

# Accessing arrays in bash requires braces (i.e. ${ } syntax).

DOTOOL_OUTPUT=$(xdotool getmouselocation)
if [[ $XDOTOOL_OUTPUT =~ window:([0-9]+) ]]; then
  WINDOW_HANDLE=${BASH_REMATCH[1]}
fi
于 2014-05-04T20:07:14.890 回答
2

:带有字段分隔符和抓取列 4的 awk

您可以使用这样的 awk 脚本

#!/bin/awk
BEGIN { FS=":";}
print $5

或在命令行上运行它。

awk -F':' '{print $5}' file

在你的情况下

xdotool getmouselocation | awk -F':' '{print $5}' -

将它设置为一个变量(这可能是你正在做的)

WINDOWLOC=`xdotool getmouselocation | awk -F':' '{print $5}' -`

或者

WINDOWLOC=$(xdotool getmouselocation | awk -F':' '{print $5}' -)
于 2014-05-04T15:40:30.593 回答
1

尝试这个,

sed 's/.*window:\(.*\)/\1/g' file  

在你的情况下,

xdotool getmouselocation | sed 's/.*window:\(.*\)/\1/g'

例子:

$ echo "x:392 y:344 screen:0 window:54799020" | sed 's/.*window:\(.*\)/\1/g'
54799020
于 2014-05-04T15:41:21.663 回答