You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将lsusb输出的打印机USB信息保存为指定格式的YAML文件

解决方法

直接用下面的完整命令,就能把提取到的打印机USB信息转换成你需要的YAML格式并保存到文件:

sed -En 's/[ 	]*b?(id[vp][^ 	]*|endpoint)(address)?[ 	]+([^ 	]*).* (out|in)?.*/\l\1\4 (\3)/Ip' <(lsusb | awk '$0 ~ /STMicroelectronics printer-80/{print $6}' | xargs -I % sh -c "lsusb -vvv -d %") | awk '
BEGIN {
    print "printer:"
    print "        type: Usb"
}
/^idVendor/ {
    printf "        idVendor: %s\n", substr($0, index($0, "(")+1, length($0)-index($0, "(")-1)
}
/^idProduct/ {
    printf "        idProduct: %s\n", substr($0, index($0, "(")+1, length($0)-index($0, "(")-1)
}
/^endpointOUT/ {
    printf "        out_ep: %s\n", substr($0, index($0, "(")+1, length($0)-index($0, "(")-1)
}
/^endpointIN/ {
    printf "        in_ep: %s\n", substr($0, index($0, "(")+1, length($0)-index($0, "(")-1)
}' > printer.yaml

命令拆解

  1. 前面的sed和lsusb组合负责从系统中提取打印机的关键USB信息,输出你提供的那四行内容。
  2. 后面的awk脚本专门做格式转换:
    • 开头先输出YAML的顶层节点printer:和固定的type: Usb字段。
    • 识别idVendor/idProduct行,直接提取括号里的数值,按要求的YAML格式输出。
    • 识别endpointOUT/endpointIN行,把字段名改成out_ep/in_ep,再提取括号里的端点值输出。
  3. 最后用> printer.yaml把结果写入目标文件,直接生成符合要求的YAML。

效果验证

执行完命令后,打开printer.yaml,内容会和你需要的完全一致:

printer:
        type: Usb
        idVendor: 0x0483
        idProduct: 0x5743
        out_ep: 0x01
        in_ep: 0x81

内容的提问来源于stack exchange,提问作者DatabaseMan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 09:35:38