我有一个脚本日志文件,看起来有点像这样:
2012-9-16
Did something
Did 345 things
Script time: 244 seconds
2012-9-17
Did yet something
Did another thing 23 times
Script time: 352 seconds
2012-9-18
Did something special for 34 seconds 51 times
Did nothing at all
Script time: 122 seconds
N
我想在行中找到最大值Script time: N seconds
。但是,我需要保留上下文,因此简单地删除所有不包含的行Script time
并不是可行的解决方案。
目前,我正在 grep 查找带有 的行Script time
,然后对这些行进行排序以找到最高值,然后返回原始文件并搜索该值。但是,如果有更直接的方法,那么我很想知道。
这是最新 CentOS 上的 Vim 7.3。如果可能的话,我更愿意继续使用 VIM。
答案1
我不确定您是否可以在 vim 中使用 shell 命令,但这是我的解决方案...有点 hacky:
cat test.txt | sed ":a;N;$!ba;s/\n\n/###/g" | sed ":a;N;$!ba;s/\n/ /g" | sed "s/###/\n/g" | sort "-nrt:" -k2 | head -1
那么...简短的解释一下:
cat test.txt # Can be omitted as sed does also accept files,
# but I like it for readability
sed ":a;N;$!ba;s/\n\n/###/g" # Replace the double-newlines with a placeholder
sed ":a;N;$!ba;s/\n/ /g" # Replace all newlines with a space
sed "s/###/\n/g" # Replace all placeholders with a newline
sort "-nrt:" -k2 # Sort numeric, reverse, use the : as delimiter and
# use the second field for sorting
head -1 # Give us only the first line
已sed
被占用来自这个 Stack Overflow 问题。
答案2
尝试awk
:
awk -vRS='' 'max<$(NF-1){max=$(NF-1);tmp=$0};END{print tmp}' input.txt
致电:awk
vim
:%!awk ...
答案3
我通过这个 VIM 函数找到了解决方案:
function Find()
execute "g!/Script/d"
execute "sort"
normal G
normal 0v$"ay
normal u
execute "call search('".@a."')"
endfunction