1

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)

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)

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.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Zoltan Szabo
  • 77
  • 1
  • 7
  • Probably the `-A` option in `grep` could help you. Check this post: http://stackoverflow.com/questions/9081/grep-a-file-but-show-several-surrounding-lines. – codeforester Mar 23 '17 at 15:38
  • 2
    `-A` is for a fixed number of lines, which is sufficient if the number of lines between two `Cache Info` lines is fixed. Otherwise, you'll need to use something like `awk`. – chepner Mar 23 '17 at 15:39

2 Answers2

1

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>

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 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.

SiKing
  • 10,003
  • 10
  • 39
  • 90
JNevill
  • 46,980
  • 4
  • 38
  • 63
  • hopefully OP easily sees the typo in the awk command--it should obviously not be "aawk". – unigeek Mar 23 '17 at 16:20
  • Yes, it worked! Thank you very much. Now i see how awesome awk really is. Question: if lets say i would search for "Max Size" only from the "Level: L1" from the same file..how wold i do it? – Zoltan Szabo Mar 24 '17 at 09:09
  • i tryed it like this, but it did not give any results: awk '$1=="Level:" {recordFound==1} recordFound==1 {print $0}' file.txt | grep -i "max. size" – Zoltan Szabo Mar 24 '17 at 09:11
  • the thing is that the file is huge...with other information..and if i do awk '$1=="Level:" && $2!="L3" {recordFound=0} $1=="Level:" && $2=="L3" {recordFound=1} recordFound==1 {print $0}' /tester/smbiosDump2.txt | grep -i "max. size" then it prints out the max size of L3 but also other Max size info i dont want.. – Zoltan Szabo Mar 24 '17 at 09:14
  • so how to get only the L1 or L2 max size info...and nobody else's max size info – Zoltan Szabo Mar 24 '17 at 09:15
  • To get the maxsize for L1 specifically for Cache #32 you could do: `awk '$1=="Max." && levelFound==1 && cacheFound{print $3" "$4; cacheFound=0; levelFound=0} $1=="Level:" && $2=="L1" {levelFound=1} $1=="Cache" && $3=="#32" {cacheFound=1}'` If this gets much bigger or more complicated, it would be a very good idea to stick this awk script in it's own `script.awk` file so proper whitespace/indententation/linefeeds can happen. – JNevill Mar 24 '17 at 13:39
0

This is a job for awk, which is made much easier if there were blank lines before each "Cache Entry" line....so insert some blank lines:

sed '/^Cache Info/i
' input | awk '$3 == "#32"' RS=

The sed is just there to insert a blank line. The blank lines are needed so that awk can treat each cache entry as a distinct record (by unsetting RS in awk, awk works with records separated by blank lines rather than individual lines.) When the 3rd field of the record is the string "#32", awk prints the record.

William Pursell
  • 204,365
  • 48
  • 270
  • 300