
次のようなスクリプト ログ ファイルがあります。
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 でシェル コマンドを使用できるかどうかはわかりませんが、これが私の解決策です...少しハッキーです:
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