Help me parse a text file and extract specific values?

I would recommend using a regular expression for this, something like: open System.Text. RegularExpressions let usernameRegex = new Regex(". *owned by\s+(?.

*)\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)).

Thanx for the awnser . Its part of the solution but I think I can manage to sort de usernames and get the printed pages per user (i think) Regards – Coolzero1974 Jul 4 at 7:50.

You can do something like: 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) ).

– Coolzero1974 Jun 16 at 19:29 I have updated my answer, please try that. This will put the user and count in each line and user and count is separated by tab – Ankur Jun 17 at 4:33 thx for tthe code but I got a lot of error System. Formatexeption: inputstring was not in the correct format – Coolzero1974 Jun 17 at 10:12 Seems that parseInt is not working because the string between pages printed\\: and .

Is not an integer – Ankur Jun 17 at 10:30 between those words it's always a number . Like pages printed: 6.No user action is required. It's standard message from log event 10 – Coolzero1974 Jun 17 at 11:38.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions


Thank You!
send