问题:我有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
Run Code Online (Sandbox Code Playgroud)
需要将此输出重定向到文本文件,我为此使用以下命令:
dmesg | grep ttyUSB > test.txt
一切正常,但我只需要列出可用端口。我希望文本文件输出是这样的:
ttyUSB0
ttyUSB1
ttyUSB2
ttyUSB3
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
只需使用此命令:
dmesg | egrep -o "ttyUSB[0-9]+" | sort -u > test.txt
Run Code Online (Sandbox Code Playgroud)
它将匹配并返回 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.
Run Code Online (Sandbox Code Playgroud)