简体   繁体   English

在有条件的awk / sed中使用正则表达式吗?

[英]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. 使用RegExr,但是我不确定如何将其添加到我的代码中。 Would it be better to add it in a conditional or just outright use awk/sed? 最好将其添加到有条件的环境中,或者直接使用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. 我不确定如何在awk / sed中使用此表达式。 The assumption is made in the expression that the first number of each section will be [0-9] which is fine. 假设在表达式中每个部分的第一个数字为[0-9],这很好。

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: 使用sed可以使用此正则表达式:

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 / tmp /下面的

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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