반복되는 문자열에서 정수의 가장 큰 값 찾기

반복되는 문자열에서 정수의 가장 큰 값 찾기

다음과 같은 스크립트 로그 파일이 있습니다.

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컨텍스트에 포함되지 않은 모든 줄을 단순히 제거하는 것은 실행 가능한 솔루션이 아닙니다.

현재는 가 있는 줄을 찾아 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이 스택 오버플로 질문에서.

답변2

노력하다 awk:

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

전화 :awkvim

:%!awk ...

답변3

이 VIM 기능으로 솔루션을 찾았습니다.

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

관련 정보