1

I'm writing a script on Linux to manage my windows based on their position and aspect. I use xwininfo -id ID -all to show window info, in particular if maximized and if fullscreen, it returns something like:

xwininfo: Window id: 0x2200001 "Newest 'bash' Questions - Unix & Linux Stack Exchange - Google Chrome"

    [......]

  Window manager hints:
      Client accepts input or input focus: Yes
      Initial state is Normal State
      Displayed on desktop 0
      Window type:
          Normal
      Window state:
          Maximized Horz
          Maximized Vert
          Fullscreen
          Focused
      Process id: 63354 on host antonio-ThinkPad

  Normal window size hints:
      Program supplied minimum size: 121 by 127
   
    [.......]

I would like to extract everything between Window manager hints: and Normal window size hints (non-inclusive) and save in a variable called, for example, $info, without losing the separate lines. I tried with awk and sed with no success. I'm not an expert programmer so thanks in advance :)

Expected output as suggested:

  Client accepts input or input focus: Yes
  Initial state is Normal State
  Displayed on desktop 0
  Window type:
      Normal
  Window state:
      Maximized Horz
      Maximized Vert
      Fullscreen
      Focused

No they don't appear anywhere else.

2
  • If you use xwininfo -all on a window that is fullscreen it returns something like this: [.....] Window manager hints: [return] Client accepts input or input focus: Yes [return] Initial state is Normal State [return] Displayed on desktop 0 [return] Window type: Normal [return] Window state: [return] Maximized Horz [return] Maximized Vert [return] Fullscreen [return] Focused [¶eturn] Process id: 63354 on host antonio-ThinkPad [return] Normal window size hints: [.....] – Antonio Micari Jun 18 '20 at 10:48
  • Are there empty lines in the relevant text part? Do you want to preserve them (if so, all, or only if not at the end)? – AdminBee Jun 18 '20 at 10:48
2

This will print what's between the two lines excluding the two lines:

awk '/Window manager hints:/{flag=1; next} /Normal window size hints/{flag=0} flag' file.txt

Output:

     Displayed on desktop 0
     Window type:
         Normal
     Window state:
         Maximized Horz
         Maximized Vert
[.......]

This will do it including the two lines:

awk '/Window manager hints:/,/Normal window size hints/' file.txt

Output:

 Window manager hints:
     Displayed on desktop 0
     Window type:
         Normal
 Window state:
     Maximized Horz
     Maximized Vert
[.......]

The commands above are run against a file but you can also pipe it if from your command:

xwininfo -id ID -all | awk '/Window manager hints:/{flag=1; next} /Normal window size hints/{flag=0} flag'

Or

xwininfo -id ID -all | '/Window manager hints:/,/Normal window size hints/'
1
  • Works very well. Thank you very much – Antonio Micari Jun 18 '20 at 11:02
1

You could try the following:

xwininfo -id ID -all | awk -v RS="" '/^ *Window manager hints/{sub(/^[^\n]*\n/,"");print}'

to extract the relevant block. This uses awk in "Paragraph mode", i.e. every "blank line-separated" block is treated as one record. Then, you only need to check if that record starts with "Window manager hints", possibly preceded with whitespace. If so, it removes the first line and prints the record, which will be the text block interesting to you.

In order to use this in a shell script and import the output in a shell variable, place it inside a "command substitution" as follows:

info="$(xwininfo -id ID -all | awk -v RS="" '/^ *Window manager hints/{sub(/^[^\n]*\n/,"");print}')"

This will preserve the full structure including newlines thanks to "correct variable quoting (TM)".

2
  • Unfortunately it doesn't work. I corrected xwiminfo with xwininfo. It returns a blank line, anyway thank you all. The other solution works well! :) – Antonio Micari Jun 18 '20 at 11:02
  • Funny. Does it work if you save the output into a file, say output.txt, and then use awk -v RS="" '(etc)' output.txt? – AdminBee Jun 18 '20 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.