2 people like it.

Log file parsing

File parsing, based on multiple lines, using recursive pattern matching with many :: (cons) operator

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
(* c:\example.txt content:

== MyItem 1 ==
...some content...
Total: 10
Success

== MyItem 2 ==
...some content...
Total: 2
Failed

== MyItem 3 ==
...some content...
Total: 14
Success

== MyItem 4 ==
...some content...
Total: 7
Failed

*)


#if INTERACTIVE
  ;;
#else
module myParser
#endif

let file = @"c:\example.txt"
let lines = System.IO.File.ReadAllLines(file)

let countFailed =
    let rec readLines (myLines:string list) resultItem resultdata =
        match myLines with
        | h::t when h.Contains("==") -> 
            let myItem = h.Substring(3, h.Length-6)
            readLines t (myItem::resultItem) resultdata
        | total::state::t when total.Contains("Total:") && state.StartsWith("Failed") -> 
            let info = (List.head resultItem), total.Substring(7) //, state ...etc information
            readLines t resultItem (info::resultdata)
        | h::t  -> readLines t resultItem resultdata
        | [] -> resultdata
    readLines (lines |> Seq.toList) [] []

// val countFailed : (string * string) list = [("MyItem 4", "7"); ("MyItem 2", "2")]
namespace System
namespace System.IO
type File =
  static member AppendAllLines : path:string * contents:IEnumerable<string> -> unit + 1 overload
  static member AppendAllText : path:string * contents:string -> unit + 1 overload
  static member AppendText : path:string -> StreamWriter
  static member Copy : sourceFileName:string * destFileName:string -> unit + 1 overload
  static member Create : path:string -> FileStream + 3 overloads
  static member CreateText : path:string -> StreamWriter
  static member Decrypt : path:string -> unit
  static member Delete : path:string -> unit
  static member Encrypt : path:string -> unit
  static member Exists : path:string -> bool
  ...

Full name: System.IO.File
System.IO.File.ReadAllLines(path: string) : string []
System.IO.File.ReadAllLines(path: string, encoding: System.Text.Encoding) : string []
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val head : list:'T list -> 'T

Full name: Microsoft.FSharp.Collections.List.head
module Seq

from Microsoft.FSharp.Collections
val toList : source:seq<'T> -> 'T list

Full name: Microsoft.FSharp.Collections.Seq.toList
Raw view Test code New version

More information

Link:http://fssnip.net/iV
Posted:10 years ago
Author:Tuomas Hietanen
Tags: log , parsing , text processing