Помогите мне разобрать текстовый файл и извлечь конкретные значения - PullRequest
0 голосов
/ 16 июня 2011

У меня есть файл с именем lijst.txt.Файл является выводом из файла журнала событий printmessage.Все строки имеют одинаковый формат.Я хочу извлечь из каждой строки имя пользователя, которое находится между словами owned by и was.Также я хочу извлечь количество страниц, которое находится между словами pages printed: и ..Я хотел бы поместить эти значения в новый текстовый файл.

С уважением,

Деннис (новинка в F #)

Ответы [ 2 ]

0 голосов
/ 02 июля 2011

Я бы порекомендовал использовать для этого регулярное выражение, например:

open System.Text.RegularExpressions

let usernameRegex = new Regex(".*owned by\s+(?<username>.*)\s+was.*")

/// Trys to extract the username from a given line of text. Returns None if the line is malformed
// Note: You could also use failwith in the else branch or throw an exception or ...
let extractUsername line = 
    let regexMatch = usernameRegex.Match(line) in
    if regexMatch.Success then Some regexMatch.Groups.["username"].Value else None

// In reality you would like to load this from file using File.ReadAllLines
let sampleLines = 
    ["Some text some text owned by DESIRED USERNAME was some text some text";
     "Some text line not containing the pattern";
     "Another line owned by ANOTHER USER was"]

let extractUsernames lines = 
    lines
    |> Seq.map extractUsername
    |> Seq.filter (fun usernameOption -> usernameOption.IsSome)
    |> Seq.map (fun usernameOption -> usernameOption.Value)

// You can now save the usernames to a file using 
// File.WriteAllLines("FileName", extractUsernames(sampleLines))
0 голосов
/ 16 июня 2011

Вы можете сделать что-то вроде:

let getBetween (a:string) (b:string) (str:string) = 
        str.Split(a.ToCharArray()).[1].Split(b.ToCharArray()).[0].Trim()  

let total (a:string seq) =
    (a |> Seq.map Int32.Parse |> Seq.reduce (+)).ToString()

File.ReadAllLines("inFile") |> Seq.map (fun l -> (getBetween "owned by" "was" l , getBetween "Pages printed:" "." l) )
|> Seq.groupBy (fun (user,count) -> user)
|> Seq.map (fun (user,counts) -> user + "\t" + (counts |> Seq.map snd |> total) )
|> (fun s -> File.WriteAllLines("outFile",s) )
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...