问题:我有dmesg | grep ttyUSB
一个具有以下输出的命令:
[ 7.648896] usb 1-2: GSM modem (1-port) converter now attached to ttyUSB0
[ 7.649091] usb 1-2: GSM modem (1-port) converter now attached to ttyUSB1
[ 7.649502] usb 1-2: GSM modem (1-port) converter now attached to ttyUSB2
[17406.327030] option1 ttyUSB0: GSM modem (1-port) converter now disconnected from ttyUSB0
[17406.329670] option1 ttyUSB1: GSM modem (1-port) converter now disconnected from ttyUSB1
[17406.334036] option1 ttyUSB2: GSM modem (1-port) converter now disconnected from ttyUSB2
[90128.405694] usb 1-4: GSM modem (1-port) converter now attached to ttyUSB0
[90128.405987] usb 1-4: GSM modem (1-port) converter now attached to ttyUSB1
[90128.406295] usb 1-4: GSM modem (1-port) converter now attached to ttyUSB2
[90132.905458] usb 1-2: GSM modem (1-port) converter now attached to ttyUSB3
[90132.905791] usb 1-2: GSM modem (1-port) converter now attached to ttyUSB4
[90132.906541] usb 1-2: GSM modem (1-port) converter now attached to ttyUSB5
[90148.661532] usb 1-1: GSM modem (1-port) converter now attached to ttyUSB6
[90148.661828] usb 1-1: GSM modem (1-port) converter now attached to ttyUSB7
[90148.662440] usb 1-1: GSM modem (1-port) converter now attached to ttyUSB8
需要将此输出重定向到文本文件,为此我使用以下命令:
dmesg | grep ttyUSB > test.txt
一切正常,但我只需要列出可用的端口。我希望文本文件输出如下:
ttyUSB0
ttyUSB1
ttyUSB2
ttyUSB3
我怎样才能做到这一点?
答案1
只需使用此命令:
dmesg | egrep -o "ttyUSB[0-9]+" | sort -u > test.txt
它将匹配并返回 ttyUSB 模式。
从man grep
:
-o, --only-matching
Print only the matched (non-empty) parts of a matching line,
with each such part on a separate output line.
答案2
我认为这个问题比宣传的要难一些。仅仅解析dmesg
实例ttyUSB*
并不能让你知道它当前是否可用。它可能刚刚被断开了。
下面通过查看当前的状态。它通过查找to ...
和实例来实现这一点,并且只使用位于行尾的from ...
那些实例。to ttyUSB*
dmesg | tac | awk '$NF~/ttyUSB[0-9]+/ && !a[$NF]++ && $(NF-1)=="to" {print $NF}'
快速解释一下:
tac
反转输入,所以我们先处理最近的$NF ~ /ttyUSB[0-9]+/
是一个正则表达式,检查最后一个字段是否是ttyUSB*
!a[$NF]++
检查我们之前是否见过此字段$(NF-1) == "to"
对倒数第二个字段进行过滤,以确保我们处理的是行to ...
。
为了测试这一点,我简单地获取了您的输出并删除了第二个连接。在我的示例中,ttyUSB2 已连接,然后断开连接,并且从未重新连接。它不应该可用。
$ cat testdmesg
[ 7.648896] usb 1-2: GSM modem (1-port) converter now attached to ttyUSB0
[ 7.649091] usb 1-2: GSM modem (1-port) converter now attached to ttyUSB1
[ 7.649502] usb 1-2: GSM modem (1-port) converter now attached to ttyUSB2
[17406.327030] option1 ttyUSB0: GSM modem (1-port) converter now disconnected from ttyUSB0
[17406.329670] option1 ttyUSB1: GSM modem (1-port) converter now disconnected from ttyUSB1
[17406.334036] option1 ttyUSB2: GSM modem (1-port) converter now disconnected from ttyUSB2
[90128.405694] usb 1-4: GSM modem (1-port) converter now attached to ttyUSB0
[90128.405987] usb 1-4: GSM modem (1-port) converter now attached to ttyUSB1
[90132.905458] usb 1-2: GSM modem (1-port) converter now attached to ttyUSB3
[90132.905791] usb 1-2: GSM modem (1-port) converter now attached to ttyUSB4
[90132.906541] usb 1-2: GSM modem (1-port) converter now attached to ttyUSB5
[90148.661532] usb 1-1: GSM modem (1-port) converter now attached to ttyUSB6
[90148.661828] usb 1-1: GSM modem (1-port) converter now attached to ttyUSB7
[90148.662440] usb 1-1: GSM modem (1-port) converter now attached to ttyUSB8
$ tac testdmesg | awk '$NF~/ttyUSB[0-9]+/ && !a[$NF]++ && $(NF-1)=="to" {print $NF}'
ttyUSB8
ttyUSB7
ttyUSB6
ttyUSB5
ttyUSB4
ttyUSB3
ttyUSB1
ttyUSB0
当然,sort
如果您愿意,您可以将其传递下去。但是您不需要uniq
(或sort -u
) 它,因为 awk 应该已经处理了重复项。