2

I have a task to receive such information from access log file (in common log format) using unix command line:

Count # of requests with status_code == 200

There is a piece of that log file (wich includes data for 1 day):

127.0.0.1 "1.1.1.1" - [02/Dec/2014:14:30:00 +0000] "POST /server/ad?uid=abc&type=INV HTTP/1.1" 200 3966 7
127.0.0.1 "1.1.1.1" - [02/Dec/2014:14:32:30 +0000] "POST /server/ad?uid=abc&type=INV HTTP/1.1" 200 3966 8
127.0.0.1 "1.1.1.1" - [02/Dec/2014:15:20:12 +0000] "POST /server/ad?uid=abc&type=INV HTTP/1.1" 200 3966 8
127.0.0.1 "1.1.1.1" - [02/Dec/2014:15:22:20 +0000] "POST /server/ad?uid=abc&type=INV HTTP/1.1" 200 3966 8
127.0.0.1 "1.1.1.1" - [02/Dec/2014:15:30:10 +0000] "POST /server/ad?uid=abc&type=INV HTTP/1.1" 200 3966 8
127.0.0.1 "1.1.1.1" - [02/Dec/2014:15:35:15 +0000] "POST /server/ad?uid=abc&type=INV HTTP/1.1" 200 3966 7
127.0.0.1 "1.1.1.1" - [02/Dec/2014:16:25:11 +0000] "POST /server/ad?uid=abc&type=INV HTTP/1.1" 200 3966 7
127.0.0.1 "1.1.1.1" - [02/Dec/2014:16:27:10 +0000] "POST /server/ad?uid=abc&type=INV HTTP/1.1" 200 3966 8
127.0.0.1 "1.1.1.1" - [02/Dec/2014:16:33:12 +0000] "POST /server/ad?uid=abc&type=INV HTTP/1.1" 200 3966 10

I use this:

$ awk -F[:\ ] '{count[$5]++}; $12 == 200 { hour[$5]++} END { for (i in hour) print i, count[i] }' acces_log.log

And receive this:

14 2
15 4
16 3

But there is a small tip: all results should be stored in file. I wonder, how can I do this from command line.

Regards

Tutor
  • 35
  • 4

1 Answers1

1

All Linux/Unix and DOS command lines understand the symbols <, <<, >, >> for redirection.

To redirect output to a file, use

awk '{....}' > outputFile
#------------^ -- redirection

This redirection will always create a new outputFile, even if one exists already.

To append (extra) data to a file use

awk '{ .... }' >> outputFile
#--------------^^ -- append

IHTH

shellter
  • 36,525
  • 7
  • 83
  • 90