
Apache 로그 파일에서 API 응답 시간을 찾아야 합니다. 응답시간과 마찬가지로 1~2초, 2~3초 정도 걸립니다. $6
는 응답 시간이며 값은 마이크로초 단위입니다.
다음 명령을 사용하려고 하는데 출력은 항상 동일합니다.
grep 17/Sep/2016:10 /access.log| awk '{print ($6 > 1000000 && 2000000 > $6)}' | wc -l
답변1
이 질문은 예제로 몇 줄을 추가하면 access.log
더 명확해질 것입니다 . 어쨌든 awk 명령은 값에 관계없이 한 줄을 인쇄하므로 $6
줄 수를 셀 때 wc -l
grep에 의해서만 결정되는 결과를 얻습니다.
$6
서로 다른 두 값 사이의 행 수를 계산하려면 다음과 같이 쓸 수 있습니다.
grep 17/Sep/2016:10 /access.log | awk '$6 > 1000000 && 2000000 > $6' | wc -l
그러나 이 파이프라인은 다소 비효율적입니다. 다음과 같이 단일 awk 명령으로 결합하는 것이 거의 항상 더 좋습니다.
awk '/17\/Sep\/2016:10/ && $6 > 1000000 && 2000000 > $6 {c++} END{print c}' access.log
경계를 포함하려면 다음을 수행할 수 있습니다.
grep 18/Sep/2016:11 /access.log | awk ' $6>=1000000 && $6<=2000000' | wc -l
또는 동등하게
awk '/18\/Sep\/2016:11/ && $6>=1000000 && $6<=2000000 {c++} END{print c}' access.log
답변2
사용행복하다(이전 Perl_6)
조건부 문자열은 첫 번째 열에서 일치하는 행만 반환합니다(필수는 아님 grep
). 이 .words
루틴은 공백으로 분할됩니다. 일치하는 줄을 ne
생략하는 대신 사용eq
:
~$ raku -ne '.put if .words[0] eq "Mar";' file
Mar 2nd 3rd 4th 5th 1000002 7th
숫자 부등식을 연결하여 여섯 번째 열에 원하는 값이 있는 행을 반환합니다.
~$ raku -ne '.put if 1000000 <= .words[5] < 3000000;' file
Feb 2nd 3rd 4th 5th 1000001 7th
Mar 2nd 3rd 4th 5th 1000002 7th
Apr 2nd 3rd 4th 5th 1000003 7th
May 2nd 3rd 4th 5th 2000001 7th
Jun 2nd 3rd 4th 5th 2000002 7th
조건문을 함께 사용하세요.
~$ raku -ne '.put if .words[0] ne "Mar" && 1000000 <= .words[5] < 3000000;' file
Feb 2nd 3rd 4th 5th 1000001 7th
Apr 2nd 3rd 4th 5th 1000003 7th
May 2nd 3rd 4th 5th 2000001 7th
Jun 2nd 3rd 4th 5th 2000002 7th
행 카운터를 최종 출력으로 사용합니다.
~$ raku -ne 'BEGIN my $line_cnt; ++$line_cnt if .words[0] ne "Mar" && 1000000 <= .words[5] < 3000000; END put $line_cnt;' file
4
입력 예:
Jan 2nd 3rd 4th 5th 999999 7th
Feb 2nd 3rd 4th 5th 1000001 7th
Mar 2nd 3rd 4th 5th 1000002 7th
Apr 2nd 3rd 4th 5th 1000003 7th
May 2nd 3rd 4th 5th 2000001 7th
Jun 2nd 3rd 4th 5th 2000002 7th
Jul 2nd 3rd 4th 5th 3000001 7th
https://docs.raku.org/언어/operators#Tight_AND_precedence
https://docs.raku.org/언어/phasers
https://raku.org
답변3
사용진주
조건부 문자열은 첫 번째 열에서 일치하는 행만 반환합니다(필수는 아님 grep
). 일치하는 줄을 ne
생략하는 대신 사용eq
:
~$ perl -lane 'print if $F[0] eq "Mar";' file
Mar 2nd 3rd 4th 5th 1000002 7th
여섯 번째 열에서 원하는 값을 가진 행에 대한 수치 부등식을 반환합니다.
~$ perl -lane 'print if $F[5] >= 1000000 && $F[5] < 3000000;' file
Feb 2nd 3rd 4th 5th 1000001 7th
Mar 2nd 3rd 4th 5th 1000002 7th
Apr 2nd 3rd 4th 5th 1000003 7th
May 2nd 3rd 4th 5th 2000001 7th
Jun 2nd 3rd 4th 5th 2000002 7th
조건문을 함께 사용하세요.
~$ perl -lane 'if (($F[0] ne "Mar") && ($F[5] >= 1000000) && ($F[5] < 3000000)) {print $_};' file
Feb 2nd 3rd 4th 5th 1000001 7th
Apr 2nd 3rd 4th 5th 1000003 7th
May 2nd 3rd 4th 5th 2000001 7th
Jun 2nd 3rd 4th 5th 2000002 7th
행 카운터를 최종 출력으로 사용합니다.
~$ perl -lane 'BEGIN {$line_cnt}; if (($F[0] ne "Mar") && ($F[5] >= 1000000) && ($F[5] < 3000000)) {++$line_cnt}; END {print $line_cnt};' file
4
입력 예:
Jan 2nd 3rd 4th 5th 999999 7th
Feb 2nd 3rd 4th 5th 1000001 7th
Mar 2nd 3rd 4th 5th 1000002 7th
Apr 2nd 3rd 4th 5th 1000003 7th
May 2nd 3rd 4th 5th 2000001 7th
Jun 2nd 3rd 4th 5th 2000002 7th
Jul 2nd 3rd 4th 5th 3000001 7th
https://en.wikibooks.org/wiki/Perl_Programming/Conditionals
https://stackoverflow.com/questions/16678272/perl-script-to-count-words-lines
https://unix.stackexchange.com/a/727962/227738