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
It’s not
grep
that changes the output, it’sswaymsg
. It detects the output is not a terminal and switches to JSON output. Try specifying the--pretty
option.swaymsg
does both plain "text" andjson
. It just defaults to plain text whenstdout
is atty
.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 asjq
to parse individualjson
components which in your case it would be super simple to granname
,type
oridentifier
rather than having toawk
an output …