gpt4 book ai didi

linux - 在 bash 中搜索和回显字符串的一部分

转载 作者:太空狗 更新时间:2023-10-29 11:38:48 25 4
gpt4 key购买 nike

(在 bash 中)在字符串中搜索给定模式并在不使用 awk 的情况下回显它的最佳方法是什么?我想使用标准命令,如 grepcatcutsed

示例:

我想从 lspci 命令的输出中去掉所有 USB Controller 的设备地址:

[artlab@localhost ~]$ lspci

00:00.0 Host bridge: Intel Corporation Core Processor DRAM Controller (rev 18)
00:1a.0 USB controller: Intel Corporation 5 Series/3400 Series Chipset USB2 Enhanced Host Controller (rev 05)
00:1c.0 PCI bridge: Intel Corporation 5 Series/3400 Series Chipset PCI Express Root Port 1 (rev 05)
00:1c.4 PCI bridge: Intel Corporation 5 Series/3400 Series Chipset PCI Express Root Port 5 (rev 05)
00:1c.5 PCI bridge: Intel Corporation 5 Series/3400 Series Chipset PCI Express Root Port 6 (rev 05)
00:1d.0 USB controller: Intel Corporation 5 Series/3400 Series Chipset USB2 Enhanced Host Controller (rev 05)
00:1e.0 PCI bridge: Intel Corporation 82801 PCI Bridge (rev a5)
00:1f.0 ISA bridge: Intel Corporation 3400 Series Chipset LPC Interface Controller (rev 05)
00:1f.2 RAID bus controller: Intel Corporation 82801 SATA Controller [RAID mode] (rev 05)
00:1f.3 SMBus: Intel Corporation 5 Series/3400 Series Chipset SMBus Controller (rev 05)
02:00.0 Ethernet controller: Intel Corporation 82574L Gigabit Network Connection
03:00.0 Ethernet controller: Intel Corporation 82574L Gigabit Network Connection
04:03.0 VGA compatible controller: Matrox Graphics, Inc. MGA G200eW WPCM450 (rev 0a)

我想检索(打印出)这两个字符串:

00:1a.0
00:1d.0

最佳答案

我不知道谁会想出更多的可能性。

lspci | grep 'USB Controller' | sed "s/ .*//"
lspci | grep 'USB Controller' | grep -o '^[^ ]*'
lspci | grep 'USB Controller' | while read id rest ; do echo $id ; done
lspci | grep 'USB Controller' | cut -f 1 -d ' '

关于linux - 在 bash 中搜索和回显字符串的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13064201/

25 4 0
文章推荐: javascript - 单击下拉菜单按钮时 Bootstrap 打开链接
文章推荐: javascript - JqueryUI Selectable - 不使用 Ctrl 取消选择
文章推荐: html - 嵌套

行不通,而嵌套

Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com