shell命令捕获特定块中的行

时间:2013-06-26 01:52:51

标签: shell grep

当我有一个类似下面的USB主机列表时,我想根据制造商过滤掉特定的块:然后获取块的UUID。 我可以使用哪些命令?

UUID:               9b379e15-83e6-406b-8ddc-46b2268d06b9
VendorId:           0x046d (046D)
ProductId:          0xc018 (C018)
Revision:           67.1 (6701)
Port:               0
USB version/speed:  2/2
Manufacturer:       Logitech
Product:            USB Optical Mouse
Address:            {745a17a0-74d3-11d0-b6fe-00a0c90f57da}\0000
Current State:      Busy

UUID:               464a8063-3f98-4b2f-885b-0168fbfd568a
VendorId:           0x04b4 (04B4)
ProductId:          0x0101 (0101)
Revision:           0.1 (0001)
Port:               0
USB version/speed:  1/1
Product:            Cypress USB Keyboard / PS2 Mouse
Address:            {36fc9e60-c465-11cf-8056-444553540000}\0050
Current State:      Busy

UUID:               41319bf4-1af5-4abb-8201-630d3e6e03eb
VendorId:           0x1004 (1004)
ProductId:          0x631f (631F)
Revision:           2.40 (0240)
Port:               0
USB version/speed:  2/2
Manufacturer:       THIS
Product:            Android Phone
SerialNumber:       
Address:            \\?\usb#vid_80ee&pid_cafe#lg-f180l-1c497609#{00873fdf-cafe-80ee-aa5e-00c04fb1720b}
Current State:      Captured

UUID:               ad6967c2-4026-496c-98e4-22033ac9a9bc
VendorId:           0x18e8 (18E8)
ProductId:          0x6260 (6260)
Revision:           5.64 (0564)
Port:               0
USB version/speed:  2/2
Manufacturer:       Broadcom Corp
Product:            Bluetooth USB
SerialNumber:       000DF09A94B8
Address:            {e0cbf06c-cd8b-4647-bb8a-263b43f0f974}\0002
Current State:      Busy

2 个答案:

答案 0 :(得分:1)

假设UUID始终是每个块的第一行:

awk 'BEGIN{RS="\n\n";FS="\n"}/Manufacturer: *Logitech/{print $1}' test.in

当然,将“Logitech”更改为您想要的任何制造商。

答案 1 :(得分:0)

我认为最简单的方法是使用像AWK或Perl这样的语言。使用Perl:

perl -nwe ' BEGIN { $/ = "" }
            next unless m/^Manufacturer: +THIS$/m;
            print "$1\n" if m/^UUID: +([0-9a-f-]+)$/m;
          ' < input_file > output_file