2

I have a bash script for hiding X11 windows. I want to be able to find what window the mouse is positioned over and unmap that window.

Using xdotool I've gotten a way to find the window ID:

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

I want to trim this line to just 54799020.
(I want to remove everything up to and including window:.)

Is there a way to do this? I have very little experience with tr and sed. I've used sed to remove text before, but I need to also remove the mouse coordinates, which are not always the same.

Wyatt Ward
  • 1,056
  • 12
  • 18

3 Answers3

2

awk with field seperator : and grab column 4

You can use an awk script like this

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

or run it on the command line.

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

and in your case

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

set it to a variable (which is probably what you are doing)

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

or

WINDOWLOC=$(xdotool getmouselocation | awk -F':' '{print $5}' -)
Hogan
  • 69,564
  • 10
  • 76
  • 117
  • This would work too, but I'm in a bash script and really don't want to make a whole other file just for this. It does work, though. +1 – Wyatt Ward May 04 '14 at 15:46
  • @Wyatt8740 You can run it as a stand alone one-liner. Check the update. – jaypal singh May 04 '14 at 15:50
  • @JS웃 - I was going to include the one-line with an edit but Amazon rang the doorbell -- thanks – Hogan May 04 '14 at 15:51
  • Already marked the other one as answer - sorry :\ I don't want to take someone's points away. But I +1ed. Thank you :) – Wyatt Ward May 04 '14 at 15:55
  • @Wyatt8740 - That is sweet -- no worries, I don't care, got plenty of points. I just like answering questions. Please enjoy SO – Hogan May 04 '14 at 16:00
  • @Hogan You're welcome. Using `PRINT` will not work due to upper-case. – jaypal singh May 04 '14 at 16:18
  • and should be `print $5` to get OP's desired output. – jaypal singh May 04 '14 at 16:26
  • 1
    @JS웃 - you're right... awk is 1 based not 0 based (`$0` is the whole line) – Hogan May 04 '14 at 16:43
  • Quick question - is `awk` usually provided in a standard GNU/linux system? I remember there was some really simple tool that ubuntu, for example, left out. maybe it was `tr`? – Wyatt Ward May 04 '14 at 19:01
  • @Wyatt8740 - awk is in everything as far as I know. (http://manpages.ubuntu.com/manpages/natty/man1/awk.1posix.html) – Hogan May 04 '14 at 23:02
  • I figured out what it was. OS X doesn't have `awk`. – Wyatt Ward Jun 19 '14 at 16:21
  • @Wyatt8740 - Maybe your machine is not setup right? --> https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/awk.1.html – Hogan Jun 19 '14 at 16:32
2

For the general case in your question title, this can be done in bash alone in at least two ways.

One uses bash string manipulation:

# ${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:}

As a mnemonic, # is to the left of $ on the keyboard and trims the start of the string; % is to the right of $ and trims the end of the string. # and % trim the shortest pattern match; ## and %% trim the longest.

The other way uses bash regular expression matching:

# 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
Jeff Bowman
  • 90,959
  • 16
  • 217
  • 251
1

Try this,

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

In your case,

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

Example:

$ echo "x:392 y:344 screen:0 window:54799020" | sed 's/.*window:\(.*\)/\1/g'
54799020
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274
  • Thanks :) This worked well. I love `sed`! (but I used `xdotool getmouselocation | sed 's/.*window:\(.*\)/\1/g'`). I can't accept this answer for 9 minutes, but I will mark it then. Until then, +1. (edit: i see you added my command to the list.) thanks! – Wyatt Ward May 04 '14 at 15:42