鉴于行分隔的文本块,我如何返回每个包含特定关键字的块? [英] Given line separated blocks of text, how can I return each block which contains a specific keyword?

查看:78
本文介绍了鉴于行分隔的文本块,我如何返回每个包含特定关键字的块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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 by Video. In other words, it returns true if a line contains the string Classes: and later contains the string Video. If this condition returns true, then awk performs the default action which is to print the record (paragraph).

这篇关于鉴于行分隔的文本块,我如何返回每个包含特定关键字的块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆