skip to Main Content

I’m using swaymsg -t get_inputs | grep to filter input device information, but I’ve noticed that grep is altering the text format.

Using grep:
$ swaymsg -t get_inputs | grep -i touch
    "identifier": "2:7:SynPS/2_Synaptics_TouchPad",
    "name": "SynPS/2 Synaptics TouchPad",
    "type": "touchpad",
Without grep:
$ swaymsg -t get_inputs

Input device: SynPS/2 Synaptics TouchPad
  Type: Touchpad
  Identifier: 2:7:SynPS/2_Synaptics_TouchPad
  Product ID: 7
  Vendor ID: 2
  Libinput Send Events: enabled

Why does this unusual behavior occur, and is there a way to make ‘grep’ preserve the text exactly as it is?

2

Answers


  1. It’s not grep that changes the output, it’s swaymsg. It detects the output is not a terminal and switches to JSON output. Try specifying the --pretty option.

    Login or Signup to reply.
  2. swaymsg does both plain "text" and json. It just defaults to plain text when stdout is a tty.

    man swaymsg
    

    You can force the plain text output with -p

    You can force the json output with -r

    The json output is useful when output to a file > your_file.json or when using a tool such as jq to parse individual json components which in your case it would be super simple to gran name, type or identifier rather than having to awk an output …

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search