我有一个包含许多块的文件,如下所示:
==9673==
==9673== HEAP SUMMARY:
==9673== in use at exit: 0 bytes in 0 blocks
==9673== total heap usage: 75,308 allocs, 75,308 frees, 7,099,382 bytes allocated
==9673==
==9673== All heap blocks were freed -- no leaks are possible
==9673==
==9673== For counts of detected and suppressed errors, rerun with: -v
==9673== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
....
....
....
....
==9655==
==9655== HEAP SUMMARY:
==9655== in use at exit: 0 bytes in 0 blocks
==9655== total heap usage: 75,308 allocs, 75,308 frees, 7,099,382 bytes allocated
==9655==
==9655== All heap blocks were freed -- no leaks are possible
==9655==
==9655== For counts of detected and suppressed errors, rerun with: -v
==9655== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
....
....
....
==9699==
==9699== HEAP SUMMARY:
==9699== in use at exit: 0 bytes in 0 blocks
==9699== total heap usage: 75,308 allocs, 75,308 frees, 7,099,382 bytes allocated
==9699==
==9699== All heap blocks were freed -- no leaks are possible
==9699==
==9699== For counts of detected and suppressed errors, rerun with: -v
==9699== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
我想从line:
开始提取最后一个块 ==XXXX== HEAP SUMMARY:
所以在我的例子中,我想只提取最后一个集团:
==9699== HEAP SUMMARY:
==9699== in use at exit: 0 bytes in 0 blocks
==9699== total heap usage: 75,308 allocs, 75,308 frees, 7,099,382 bytes allocated
==9699==
==9699== All heap blocks were freed -- no leaks are possible
==9699==
==9699== For counts of detected and suppressed errors, rerun with: -v
==9699== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
我怎么能用bash做到这一点?
答案 0 :(得分:1)
使用grep -zoP
和负前瞻正则表达式:
grep -zoP '==\w{4}== HEAP SUMMARY:(?![\s\S]*==\w{4}== HEAP SUMMARY:)[\s\S]*\z' file
==9699== HEAP SUMMARY:
==9699== in use at exit: 0 bytes in 0 blocks
==9699== total heap usage: 75,308 allocs, 75,308 frees, 7,099,382 bytes allocated
==9699==
==9699== All heap blocks were freed -- no leaks are possible
==9699==
==9699== For counts of detected and suppressed errors, rerun with: -v
==9699== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
-z
将文件数据视为空终止而不是新行终止(?![\s\S]*==\w{4}== HEAP SUMMARY:)
是负面预测,声称我们在下面的文件中没有相同的其他实例。答案 1 :(得分:1)
如果你有tac
,这可能是最简单的
$ tac file | awk '1; /==....== HEAP SUMMARY/{exit}' | tac
答案 2 :(得分:1)
如果你知道这些块总是9行,你只需使用tail
:
tail -n9 file
答案 3 :(得分:1)
使用sed:
$ sed -n '/HEAP SUMMARY/{:a;/ERROR SUMMARY/bb;N;ba;:b;$p;d}' infile
==9699== HEAP SUMMARY:
==9699== in use at exit: 0 bytes in 0 blocks
==9699== total heap usage: 75,308 allocs, 75,308 frees, 7,099,382 bytes allocated
==9699==
==9699== All heap blocks were freed -- no leaks are possible
==9699==
==9699== For counts of detected and suppressed errors, rerun with: -v
==9699== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
这是如何工作的:
sed -n ' # Do not print lines at end of each cycle
/HEAP SUMMARY/ { # If line matches "HEAP SUMMARY"
:a # Label to jump back to
/ERROR SUMMARY/bb # If line matches "ERROR SUMMARY", jump to :b
N # Append next line to pattern space
ba # Jump to :a
:b # Label to jump forward to
$p # If we are on the last line, print pattern space
d # Delete pattern space
}
' infile
每次遇到HEAP SUMMARY
时,它会将所有直到下一个ERROR SUMMARY
的行读入模式空间。然后,它检查是否已到达最后一行;如果是,则打印图案空间,否则将被删除。
答案 4 :(得分:0)
如果文件的最后一行也有块号,这将获得快速的块号(不读取整个文件以找到哪个号码):
n="$(tail -n1 infile | awk '{print $1}')"
然后我们可以从最终选择具有此类块号的所有行:
tac infile | awk -vn="$n" '!($1~n){exit};1'| tac
答案 5 :(得分:0)
这可能适合你(GNU sed):
sed '/HEAP SUMMARY:/h;//!H;$!d;x' file
当遇到HEAP SUMMARY:
时,用当前行替换保留空间(HS)中的任何内容。对于任何其他模式附加到HS的线。当模式空间(PS)与HS交换并打印出PS时,删除除最后一行之外的所有行。
答案 6 :(得分:0)
使用数据前面的数字作为ID /组号:
id=$(tail -n1 file | grep -Po '(?<=\=\=)[0-9]*') && grep "$id" file |tail -n+2