使用f#解析日志文件

时间:2015-11-10 20:49:53

标签: f# f#-3.0

我试图解析iis日志文件中的数据。

每行都有一个我需要的日期:

u_ex15090503.log:3040:2015-09-05 03:57:45

我需要的名称和电子邮件地址:

&actor=%7B%22name%22%3A%5B%22James%2C%20Smith%22%5D%2C%22mbox%22%3A%5B%22mailto%3AJames.Smith%40student.colled.edu%22%5D%7D&

我从这样得到正确的列开始。这部分工作正常。

  //get the correct column 
  let getCol = 
    let line = fileReader inputFile 
    line
    |> Seq.filter (fun line -> not (line.StartsWith("#")))
    |> Seq.map (fun line -> line.Split()) 
    |> Seq.map (fun line -> line.[7],1)
    |> Seq.toArray 
  getCol

现在我需要解析上面的内容并获取日期,姓名和电子邮件,但我很难搞清楚如何做到这一点。

到目前为止,我有这个,这给了我2个错误(下面):

  //split the above column at every "&"
  let getDataInCol =
    let line = getCol
    line
    |> Seq.map (fun line -> line.Split('&'))
    |> Seq.map (fun line -> line.[5], 1)
    |> Seq.toArray
  getDataInCol


  Seq.map (fun line -> line.Split('&'))
  the field constructor 'Split' is not defined

错误:

  Seq.map (fun line -> line.[5], 1)
  the operator 'expr.[idx]' has been used on an object of indeterminate type based on information prior to this program point.

也许我说这一切都错了。我对f#很新,所以我为草率的代码道歉。

1 个答案:

答案 0 :(得分:3)

这样的东西会得到名字和电子邮件。您仍然需要解析日期。

#r "Newtonsoft.Json.dll"

open System
open System.Text.RegularExpressions
open Newtonsoft.Json.Linq

let (|Regex|_|) pattern input =
    let m = Regex.Match(input, pattern)
    if m.Success then Some(List.tail [ for g in m.Groups -> g.Value ])
    else None

type ActorDetails =
    {
        Date: DateTime
        Name: string
        Email: string
    }

let parseActorDetails queryString =
    match queryString with
    | Regex @"[\?|&]actor=([^&]+)" [json] ->
        let jsonValue = JValue.Parse(Uri.UnescapeDataString(json))
        {
            Date = DateTime.UtcNow (* replace with parsed date *)
            Name = jsonValue.Value<JArray>("name").[0].Value<string>()
            Email = jsonValue.Value<JArray>("mbox").[0].Value<string>().[7..]
        }
    | _ -> invalidArg "queryString" "Invalid format"

parseActorDetails "&actor=%7B%22name%22%3A%5B%22James%2C%20Smith%22%5D%2C%22mbox%22%3A%5B%22mailto%3AJames.Smith%40student.colled.edu%22%5D%7D&"

val it : ActorDetails = {Date = 11/10/2015 9:14:25 PM;
                         Name = "James, Smith";
                         Email = "James.Smith@student.colled.edu";}
相关问题