如何仅匹配一行就提取文件的一部分? [英] How can I extract a section of a file based on matching only one line?

查看:38
本文介绍了如何仅匹配一行就提取文件的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Linux的新手.并已收到分配从文件中提取某些信息的任务.该文件是txt文件.它包含以下信息:

I'm rather new to Linux. And have received an assignment to extract a certain information from a file. The file is a txt file. and it contains the following information:

Cache Info: #31
Designation: "L1 Cache"
Level: L1
State: Enabled
Mode: 0x00 (Write Through)
Location: 0x00 (Internal, Not Socketed)
ECC: 0x02 (Unknown)
Type: 0x04 (Data)
Associativity: 0x07 (8-way Set-Associative)
Max. Size: 128 kB
Current Size: 128 kB
Supported SRAM Types: 0x0002 (Unknown)
Current SRAM Type: 0x0002 (Unknown)
Cache Info: #32
Designation: "L1 Cache"
Level: L1
State: Enabled
Mode: 0x00 (Write Through)
Location: 0x00 (Internal, Not Socketed)
ECC: 0x02 (Unknown)
Type: 0x03 (Instruction)
Associativity: 0x07 (8-way Set-Associative)
Max. Size: 128 kB
Current Size: 128 kB
Supported SRAM Types: 0x0002 (Unknown)
Current SRAM Type: 0x0002 (Unknown)
Cache Info: #33
Designation: "L2 Cache"
Level: L2
State: Enabled
Mode: 0x00 (Write Through)
Location: 0x00 (Internal, Not Socketed)
ECC: 0x05 (Single-bit)
Type: 0x05 (Unified)
Associativity: 0x05 (4-way Set-Associative)
Max. Size: 1024 kB
Current Size: 1024 kB
Supported SRAM Types: 0x0002 (Unknown)
Current SRAM Type: 0x0002 (Unknown)
Cache Info: #34
Designation: "L3 Cache"
Level: L3
State: Enabled
Mode: 0x01 (Write Back)
Location: 0x00 (Internal, Not Socketed)
ECC: 0x05 (Single-bit)
Type: 0x05 (Unified)
Associativity: 0x09 (12-way Set-Associative)
Max. Size: 6144 kB
Current Size: 6144 kB
Supported SRAM Types: 0x0002 (Unknown)
Current SRAM Type: 0x0002 (Unknown)

我的任务是以某种方式将"Cache Info:#32"及其所有信息提取并打印到外壳中.就像这样:

My assignment is to somehow extract and print it to the shell the "Cache Info: #32" with all of it's information. Just like so:

Cache Info: #32
Designation: "L1 Cache"
Level: L1
State: Enabled
Mode: 0x00 (Write Through)
Location: 0x00 (Internal, Not Socketed)
ECC: 0x02 (Unknown)
Type: 0x03 (Instruction)
Associativity: 0x07 (8-way Set-Associative)
Max. Size: 128 kB
Current Size: 128 kB
Supported SRAM Types: 0x0002 (Unknown)
Current SRAM Type: 0x0002 (Unknown)

我尝试使用cat/folder/file.txt |grep -i缓存信息:",但我只得到第一行,仅此而已.

I have tried with cat /folder/file.txt | grep -i "Cache Info:" but i only get the first line and nothing more.

有人可以帮我解决这个小问题吗?*此外,我想指出该文件是随机的.它可能包含更多或更少的数据.

Could anyone please help me with this small issue? *Also, i would like to note that the file is random..it could contain alot more data or a lot less.

推荐答案

使用 grep ,您可以指定要提取的搜索词"B"之前或"A"之后的记录数量.如果记录数始终是静态的,这将是非常快的:

With grep you can specify how many records "B"efore or "A"fter the search term you want to extract. If the number of records is always static, this is super quick:

 grep -A12 "#32" <yourfile>

如果记录数是可变的,则可以使用awk:

If the number of records is variable, you can use awk:

 awk '$1=="Cache" && $3!="#32" {recordFound=0} $1=="Cache" && $3=="#32" {recordFound=1} recordFound==1 {print $0}' <yourfile>

Awk将把每条记录分成多个字段.这将测试字段以查看我们是否在缓存"记录中,以及该缓存记录是否为#32".它根据该搜索将一个名为"recordFound"的变量设置为true或false.如果该变量为true,则将打印记录.

Awk will split each record up into fields. This tests the fields to see if we are at a 'cache' record and if that cache record is '#32'. It sets a variable called 'recordFound' to true or false based on that search. If the variable is true, it prints the record.

这篇关于如何仅匹配一行就提取文件的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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