简体   繁体   中英

Using a regex in a conditional vs. an outright awk/sed?

I have the line:

03:1b.0 USB Controller: Intel... (lspci -k output) 

I'm trying to use regex to check if the first field of the input line follows the form ??:??.?, but not sure how to implement it. I came up with the expression:

([0-9])\w+:([0-9])\w+.([0-9])

with RegExr, but I'm not sure how to add it to my code. Would it be better to add it in a conditional or just outright use awk/sed? I've tried to do

if [[ $input =~ /([0-9])\w+:([0-9])\w+.([0-9])/g ]]; then...

but that didn't work nor did it throw errors. I'm not sure how to use this expression with awk/sed. The assumption is made in the expression that the first number of each section will be [0-9] which is fine.

Thanks for your help!

Edit: there exist cases where the form does not exist. Let the input be:

Classes: Audio Video
Drivers: snd-usb-audio uvcvideo

00:03.0 Audio device: Intel Corporation Xeon E3-1200 v3/4th Gen Core Processor HD Audio Controller (rev 06)
    Subsystem: Gigabyte Technology Co., Ltd Device 5000
    Kernel driver in use: snd_hda_intel

Using sed you can use this regex:

s='03:1b.0 USB Controller: Intel... (lspci -k output)'
grep -E '^[0-9]\w:[0-9]\w\.[0-9]' <<< "$s"
03:1b.0 USB Controller: Intel... (lspci -k output)

What about this? /tmp/line below

03:1b.0 USB Controller: Intel... (lspci -k output)
03:kb.0 USB Controller: Intel... (lspci -k output)
03:1b.1 USB Controller: Intel... (lspci -k output)
10:1b.1 USB Controller: Intel... (lspci -k output)


gawk '/^[0-9][0-9]:[0-9][a-z].[0-9]/' /tmp/line

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM