Monday, May 13, 2024
7
rated 0 times [  7] [ 0]  / answers: 1 / hits: 4727  / 3 Years ago, tue, october 5, 2021, 7:00:33

Problem: I have dmesg | grep ttyUSB command that has the following output:



[    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


need to redirect this output to a text file, I am using the following command for this:



dmesg | grep ttyUSB > test.txt



Everything works fine, but I need only list the available ports. I wish the text file output to be like this:



ttyUSB0 
ttyUSB1
ttyUSB2
ttyUSB3


How can I do this?


More From » command-line

 Answers
6

Just use this command:



dmesg | egrep -o "ttyUSB[0-9]+" | sort -u > test.txt


It will match and return the ttyUSB patterns.



From 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.

[#25715] Wednesday, October 6, 2021, 3 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
eryeath

Total Points: 122
Total Questions: 121
Total Answers: 112

Location: Saint Helena
Member since Fri, Aug 26, 2022
2 Years ago
;