gpt4 book ai didi

linux - 如何根据仅匹配一行来提取文件的一部分?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:40:49 27 4
gpt4 key购买 nike

我是 Linux 的新手。并收到了从文件中提取特定信息的任务。该文件是一个txt文件。它包含以下信息:

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)

我的任务是以某种方式提取“缓存信息:#32”及其所有信息并将其打印到 shell。就像这样:

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 "Cache Info:"但我只得到第一行,仅此而已。

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

最佳答案

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

 grep -A12 "#32" <yourfile>

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

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

Awk 会将每条记录拆分成字段。这将测试字段以查看我们是否处于“缓存”记录以及该缓存记录是否为“#32”。它根据该搜索将名为“recordFound”的变量设置为 true 或 false。如果变量为真,它会打印记录。

关于linux - 如何根据仅匹配一行来提取文件的一部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42980432/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com