鉴于行分隔的文本块,我如何返回每个包含特定关键字的块? [英] Given line separated blocks of text, how can I return each block which contains a specific keyword?
问题描述
me $ cat file.txt
文件名:1
组织:总线1 =>端口1 => Subport 1 =>设备3
类:接口视频
驱动程序:usbhid
文件名:2
组织:总线1 =>端口1 => Subport 3 =>设备4
类别:音频
驱动程序:usb-audio uvc视频
文件名:3
组织:总线1 =>端口1 => Subport 4 =>设备5
类别:无线视频
驱动程序:uvc
如何操作这返回包含任何给定关键字的块?
例如:bash this --classVideo会返回:
文件名:1
组织:总线1 =>端口1 => Subport 1 =>设备3
类:接口视频
驱动程序:usbhid
文件名:3
组织:总线1 =>端口1 => Subport 4 =>设备5
类:无线视频
驱动程序:uvc
将脚本写入对--class和--driver情况进行排序将会很简单,只是特别要求对文本进行操作。我正在考虑grep Class:* $ input,取出行号,并在之前的换行符之前通过下一个换行符打印所有内容,但这会产生更多复杂性,可能不是最有效的方法做到这一点。
获取类...视频:
$ awk -v RS ='/ Classes:[^ \\\
] * Video /'file.txt
文件名:1
组织:Bus 1 =>端口1 => Subport 1 =>设备3
类:接口视频
驱动程序:usbhid
文件名:3
组织:总线1 =>端口1 => Subport 4 =>设备5
类:无线视频
驱动程序:uvc
与mawk和GNU awk,它应该可以与任何POSIX awk一起使用[帽子提示:ninjalj),但它可能与 这将记录分隔符设置为一个空字符串,通常被解释为含义段落。
它是如何工作的
-v RS =
类别:
,则返回true,后面跟随任意数量的 Video
。换句话说,如果行包含字符串 Classes:
,并且后面包含字符串 Video
。如果这个条件返回true,那么awk会执行打印记录(段落)的默认动作。
me$ cat file.txt
Filename: 1
Organization: Bus 1 => Port 1 => Subport 1 => Device 3
Classes: Interface Video
Drivers: usbhid
Filename: 2
Organization: Bus 1 => Port 1 => Subport 3 => Device 4
Classes: Audio
Drivers: usb-audio uvc Video
Filename: 3
Organization: Bus 1 => Port 1 => Subport 4 => Device 5
Classes: Wireless Video
Drivers: uvc
How can I manipulate this to return the blocks which contain any given keyword?
For example: bash this --class "Video" would return:
Filename: 1
Organization: Bus 1 => Port 1 => Subport 1 => Device 3
Classes: Interface Video
Drivers: usbhid
Filename: 3
Organization: Bus 1 => Port 1 => Subport 4 => Device 5
Classes: Wireless Video
Drivers: uvc
Writing the script to sort the --class and --driver case would be trivial, just asking particularly about the text manipulation. I'm thinking along the lines of grep Class:*$input, taking the line number, and printing everything from a a newline prior to that through the next newline, but that would have more complications and probably isn't the most efficient way to do it.
To get Classes...Video:
$ awk -v RS= '/Classes:[^\n]*Video/' file.txt
Filename: 1
Organization: Bus 1 => Port 1 => Subport 1 => Device 3
Classes: Interface Video
Drivers: usbhid
Filename: 3
Organization: Bus 1 => Port 1 => Subport 4 => Device 5
Classes: Wireless Video
Drivers: uvc
(I tested this with mawk and GNU awk and it should work with any POSIX awk [hat tip: ninjalj) but it may not be compatible with very old versions of awk.)
How it works
-v RS=
This sets the record separator to be an empty string which is generally interpreted as meaning paragraphs.
/Classes:[^\n]*Video/
This returns true if the record (paragraph) contains
Classes:
, followed by any number of any characters except newline, followed byVideo
. In other words, it returns true if a line contains the stringClasses:
and later contains the stringVideo
. If this condition returns true, then awk performs the default action which is to print the record (paragraph).
这篇关于鉴于行分隔的文本块,我如何返回每个包含特定关键字的块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!