이걸 awk 로 간단히 작성할수 있는지요?

hurryon의 이미지

입력 파일의 내용은 다음과 같습니다.

03:21:05        All Clients Now Started 
03:21:07    Request 2113
03:21:07    Request 2143
03:21:07    Request 2173
03:21:08    Request 3185
03:21:09    Request 3265
03:21:09    Request 3285
03:21:09    Request 3285
03:21:09    Request 3305
03:21:09    Request 3305
03:21:09    Request 3305
03:21:09    Request 3365
03:21:09    Request 3365
03:21:09    Request 3385
03:21:09    Request 4206
03:21:11    Request 5378
03:21:11    Request 5608
03:21:11    Request 5959
03:21:12    Request 6810
03:21:12    Request 6810
03:21:12    Request 6810
03:21:12    Request 6820
03:21:12    Request 6820
03:21:12    Request 6840
03:21:12    Request 40
03:21:12    Request 40
03:21:12    Request 6840
03:21:12    Request 50
03:21:12    Request 30
03:21:12    Request 30
03:21:12    Request 20
03:21:12    Request 10
03:21:12    Request 6870
03:21:12    Request 30
03:21:12    Request 60
03:21:12    Request 50

시간 대 별루 Request 다음에 나오는 숫자을 더해서 합계과 평균을 출력하려고 합니다. 뭐 출력 양식이야 <시간> <합계> <평균> 이런식으로 말입니다. C 로 작성하긴 했는데...간단히 awk 같은 녀석으로 처리할수 있을텐데 하고 생각하기에...내공을 얻고자 이렇게 글을 올립니다.

:-)

pynoos의 이미지

awk '/Request/ {s += $3 } END {print "SUM:", s}'

agkrwyasym의 이미지

위 답변에 평균이 빠졌군요.

awk '{s += $3 ; cnt += 1} END {print s, s/cnt}'

redpine50의 이미지

awk 'BEGIN {
prevtm = "NA"; sum = 0; nline = 0
print "시간  합계  평균"
}
/Request/ {
    tm = substr($1, 0, 2)
    if (tm == prevtm || prevtm == "NA") {
        sum += $3
        nline++
    } else {
        printf("%s %6d %12f\n", prevtm, sum, sum/nline)

        sum = $3; nline = 1
    }
    prevtm = tm
}
END {
       printf("%s %d %f\n", prevtm, sum, sum/nline)
}'  datafile

Life is tragedy when seen in close-up, but a comedy in long-shot.

hurryon의 이미지

으흠. 제가 설명이 부족했던 모양이군요...<시간> <합계> <평균> 과 같은 식이라고 했는데 <총합계> <총평균> 으로 이해을 하신듯...합니다.

시간이 초 단위로 되어 있는데 이것들에 대한 결과을...뽑아내는 것을 원하는것인데 말입니다.

가령 예을 들어...

Quote:

03:21:05
03:21:07 2000 10
03:21:08 3000 30
03:21:09 4000 10

과 같은 식으로 말이죠. 냠냠...이걸 못해서 간만에 C 코드로 1시간을 넘게 했기 때문에 혹시 다음번에도 이런 로그파일을 파싱해야 할 일이 생기면 awk 로 멋지게 작업하고 싶습니다. :-)

pynoos의 이미지

awk '/Request/ { if( t != $1 ) { if(cnt) print(t, s, s/cnt); t= $1; s=0; cnt=0}; s += $3; cnt += 1 } END {print t, s, s/cnt}'

익명 사용자의 이미지

awk의 강력함을 한 수 배워갑니다~~~

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.