为什么“grep”会更改 Swaymsg 输出中的文本格式?

问题描述 投票:0回答:2

我正在使用

swaymsg -t get_inputs | grep
来过滤输入设备信息,但我注意到
grep
正在更改文本格式。

使用 grep:
$ swaymsg -t get_inputs | grep -i touch
    "identifier": "2:7:SynPS\/2_Synaptics_TouchPad",
    "name": "SynPS\/2 Synaptics TouchPad",
    "type": "touchpad",
没有 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

为什么会出现这种不寻常的行为,有没有办法让“grep”保留文本原样?

json regex linux text grep
2个回答
1
投票

改变输出的不是

grep
,而是
swaymsg
。它检测到输出不是终端并切换到 JSON 输出。尝试指定
--pretty
选项。


0
投票

swaymsg
既可以做纯“文本”,又可以做
json
。当
stdout
tty
时,它只是默认为纯文本。

man swaymsg

您可以使用

-p

强制纯文本输出

您可以使用

json
 强制输出 
-r

当输出到文件

json
或使用
 > your_file.json
等工具解析各个
jq
组件时,
json
输出非常有用。

© www.soinside.com 2019 - 2024. All rights reserved.