简体   繁体   中英

How to read input by newline OR a character?

Sample input:

Filename: 1-1.3
Organization: Bus 1 => Port 1 => Subport 3 => Device 4
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
--
00:1b.0 Audio device: Intel Corporation 8 Series/C220 Series Chipset High Definition Audio Controller (rev 05)
    Subsystem: Gigabyte Technology Co., Ltd Device fa60
    Kernel driver in use: snd_hda_intel

I want to manipulate this somehow to return:

1-1.3
00:03.0
00:1b:0

I was thinking I can read by line and check for \\n or --, then hardcode either the second field (awk) if the first word is "Filename:" or just the first word if it's in the form ??:??.?. But that's another issue--can I check if the first word is in the form of ??:??.? without knowing any of the numbers? Being able to do this would also basically solve the issue because I could just parse through the first couple fields everywhere.

You can do exactly what you have described in awk:

awk -F" " '$1 == "Filename:"{print $2}
           $1 ~ /[[:alnum:]]{2}:[[:alnum:]]{2}.[[:alnum:]]/{print $1}' test

The first line checks whether the first field starts equals Filename: . If this is true, we print the second field.
The second line checks whether the first field matches the pattern "2 alphanumeric characters followed by : followed by 2 alphanumeric characters followed by . followed by one alphanumeric character". In this case, awk prints this pattern.
The output for the example provided is (as requested):

1-1.3
00:03.0
00:1b.0

Please note that SO is no coding service and that you better provide your own attempts.

Update :
According to anishsane (see comment) there is even a better regex for pattern matching. By replacing [:alnum:] by [:xdigit:] it is possible to match characters that are hexadecimal digits:

awk -F" " '$1 == "Filename:"{print $2}
           $1 ~ /[[:xdigit:]]{2}:[[:xdigit:]]{2}.[[:digit:]]/{print $1}'

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