로그에서 최대값을 얻는 방법

로그에서 최대값을 얻는 방법

10개의 값을 보여주는 로그 파일이 있고 마지막 열 값은 바이트 단위의 메모리입니다. 지난 4일 동안 사용된 최대 메모리를 표시하려면 어떤 명령이나 스크립트를 사용해야 합니까? curl또는 명령을 사용하여 지난 awk4일 동안 사용된 최대 메모리 값을 얻는 방법은 무엇입니까 ?

로그 파일의 이름은 이며 ansh.log다음 내용을 포함합니다.

 Timestamp ,xyz=1, abc=2, def=6 ,memory=64357890
Timestamp ,xyz=1 ,abc =2 , def =6 , memory=64987201

이제 마지막 열( )의 가장 높은 값을 원합니다 memory. 이 로그 파일에는 지난 4일간의 로그 줄이 포함되어 있으므로 파일 크기가 매우 큽니다.

답변1

최대값만 찾고 있으므로 정렬이 필요하지 않고 파일만 스캔하면 됩니다.

awk -F "=" '
    $NF > max {max = $NF}
    END {print max}
' ansh.log

한 줄이 마음에 든다면

awk -F= '$NF > max {max = $NF} END {print max}' ansh.log

답변2

sed/cut/awk를 사용하여 원하는 대로 분할한 다음 답변을 정렬하여 마지막 답변만 표시한 다음 가장 높은 답변을 얻을 수 있습니다.

하위 명령이 많은 예는 읽기가 더 쉽습니다.

$
 cat > /tmp/a #define a fake-file for test/demo
Timestamp ,xyz=1, abc=2, def=6 ,memory=64357890 Timestamp ,xyz=1 ,abc =2 , def =6 , memory=64987201
Timestamp ,xyz=1, abc=2, def=6 ,memory=64357890 Timestamp ,xyz=1 ,abc =2 , def =6 , memory=64987555
Timestamp ,xyz=1, abc=2, def=6 ,memory=64357890 Timestamp ,xyz=1 ,abc =2 , def =6 , memory=64798797
Timestamp ,xyz=1, abc=2, def=6 ,memory=64357890 Timestamp ,xyz=1 ,abc =2 , def =6 , memory=64911111
Timestamp ,xyz=1, abc=2, def=6 ,memory=64357890 Timestamp ,xyz=1 ,abc =2 , def =6 , memory=61111111
$
  sed "s/ Time/\nTime/1 ; s/.*=//" /tmp/a | sort -n | tail -1 # one line for each timestamp even we have many on same line / then get only number / then sort numbers / then keep only the last one (higher cause they are sorted)
64987555 
$
 variable=$(sed "s/ Time/\nTime/1 ; s/.*=//" /tmp/a | sort -n | tail -1)  # if your want that in a variable only
$
 echo ${variable}
64987555
$
 rm /tmp/a  # remove test-file 

답변3

@glennjackman은 이미 awk 스크립트를 다루었으므로 =입력 예제에 표시된 것처럼 각 줄에 항상 4개가 있다고 가정하면 실제로 매우 간결하고 효율적인 대안이 있습니다.

$ cut -d'=' -f5 file | sort -rn | head -1
64987201

수량이 가변적인 경우 언제든지 이 명령을 로 바꿀 =수 있습니다 .cutawk -F'=' '{print $NF}'

답변4

다음은 Ed Morton의 답변과 유사한 또 다른 답변이지만 고정된 금액이 필요하지는 않습니다 =.

grep -oh "memory.[0-9,=]*" file | cut -d'=' -f2 | sort -rn | head -1

관련 정보