Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a script log file that looks a bit like this:

Did something
Did 345 things
Script time: 244 seconds

Did yet something
Did another thing 23 times
Script time: 352 seconds

Did something special for 34 seconds 51 times
Did nothing at all
Script time: 122 seconds

I would like to find the largest value of N in the lines Script time: N seconds. However, I need to keep the context, so simply removing all lines that don't contain Script time in them is not a viable solution.

Currently, I am grepping for lines with Script time, then sorting those to find the highest value, then going back to the original file and searching for that value. However, if there is a more straightforward way then I would love to know.

This is on Vim 7.3 on a recent CentOS. I would prefer to remain in VIM if possible. Thanks.

share|improve this question

I'm not sure if you can use a shell command in vim, but this would be my solution...little bit 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

Soooo...short explanation:

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

The sed is taken from this Stack Overflow question.

share|improve this answer
Thank you, that is an interesting one-liner! However, it does only return the line but not the context, which can actually be gotten quite simply: cat t | grep "Script time:" | sort -r | head -n 1. As mentioned in the OP, I'm looking to add context (the associated log entry) to that! – dotancohen Sep 19 '12 at 20:48
@dotancohen: Huh? This returns the whole 4-line block in one line (sorry for that) least it did on my machine. – Bobby Sep 20 '12 at 6:37

Try awk:

awk -vRS='' 'max<$(NF-1){max=$(NF-1);tmp=$0};END{print tmp}' input.txt

Call awk in vim:

:%!awk ...
share|improve this answer
up vote 0 down vote accepted

I found the solution with this VIM function:

function Find()
    execute "g!/Script/d"
    execute "sort"
    normal G
    normal 0v$"ay
    normal u
    execute "call search('".@a."')"
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.