English 中文(简体)
正在分析文本文件行
原标题:Parsing text file lines

我有一个日志文件,其中除了其他数据外, 包含了这样的线条:

2012-05-23T20:52:11+00:00 heroku[router]: GET myapp.com/practitioner_activities/10471/edit dyno=web.2 queue=0 wait=0ms service=866ms status=200 bytes=48799
2012-05-23T20:52:46+00:00 heroku[router]: GET myapp.com/users/sign_out dyno=web.1 queue=0 wait=0ms service=20ms status=302 bytes=88
2012-05-23T20:52:46+00:00 heroku[router]: GET myapp.com/ dyno=web.13 queue=0 wait=0ms service=18ms status=200 bytes=4680
2012-05-23T20:53:04+00:00 heroku[router]: POST myapp.com/p/ENaCXExu7qNEqzwYYyPs dyno=web.5 queue=0 wait=0ms service=207ms status=302 bytes=119
2012-05-23T20:53:04+00:00 heroku[router]: GET myapp.com/practitioner_activities/welcome dyno=web.3 queue=0 wait=0ms service=57ms status=200 bytes=5061
2012-05-23T20:53:04+00:00 heroku[router]: GET myapp.com/assets/application-print-715276cc0b76d0d82db3ab5866f22a23.css dyno=web.14 queue=0 wait=0ms service=9ms status=200 bytes=76386

我想分析它们,然后把它们放入一个文件,我可以用出色的分析来打开。我需要时间、分钟、通晓(GET 或 POST ) 、 URL 和 服务时间 。

例如,以上第一行:

2012-05-23T20:52:11+00:00 heroku[router]: GET myapp.com/practitioner_activities/10471/edit dyno=web.2 queue=0 wait=0ms service=866ms status=200 bytes=48799

我期望产出看起来会像:

"20", "52", "GET", "myapp.com/practitioner_activities/10471/edit", "866"

我该如何在 awk 或用短红宝石脚本做到这一点?

最佳回答

使用 awk , 您可以尝试类似 :

awk  { OFS="", ""; split ($8, array, "="); printf """ substr ($1 , length ($1) - 13, 2 ) OFS substr ($1 , length ($1) - 10, 2 ) OFS $3 OFS $4 OFS substr (array[2], 0, length (array[2]) -2) ""
" }  file.txt

结果:

"20", "52", "GET", "myapp.com/practitioner_activities/10471/edit", "866"
"20", "52", "GET", "myapp.com/users/sign_out", "20"
"20", "52", "GET", "myapp.com/", "18"
"20", "53", "POST", "myapp.com/p/ENaCXExu7qNEqzwYYyPs", "207"
"20", "53", "GET", "myapp.com/practitioner_activities/welcome", "57"
"20", "53", "GET", "myapp.com/assets/application-print-715276cc0b76d0d82db3ab5866f22a23.css", "9"

HTH, HTH, HTH, HTH, HTH, HTH, HTH, HTH, HT, HTH

<强>编辑:

awk  { OFS="", ""; ORS=""
"; split ($8, array, "="); print """ substr ($1 , 12, 2 ), substr ($1 , 15, 2 ), $3, $4, array[2] + 0 }  file.txt

丹尼斯,谢谢!

问题回答

红宝石答案

ruby -ane  
    hr, min = $F[0][/(?<=T)dd:dd/].split(/:/)
    svc = $F[7].split(/=/)[-1]; svc[/ms/] = ""
    puts %q{"%d", "%d", "%s", "%s", "%d"} % [hr, min, $F[2], $F[3], svc]
  logfile




相关问题
What does it mean "to write a web service"?

I just asked a question about whether it was possible to write a web-page-checking code and run it from free web server, and one supporter answered and said that it was possible only if I run "a web ...

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Dynamically building a command in bash

I am construcing a command in bash dynamically. This works fine: COMMAND="java myclass" ${COMMAND} Now I want to dynamically construct a command that redirectes the output: LOG=">> myfile.log ...

Why does Scala create a ~/tmp directory when I run a script?

When I execute a Scala script from the command line, a directory named "tmp" is created in my home directory. It is always empty, so I simply deleted it without any apparent problem. Of course, when I ...

Ivy, ant and start scripts

I have a project that uses ant to build and ivy for dependencies. I would like to generate the start scripts for my project, with the classpath, based on the dependencies configured in Ivy, ...

热门标签