DPyro Posted November 18, 2012 Share Posted November 18, 2012 I am reading the StandardOutput of a process to a string and would like to match certain text and write the full line to a text file. For example: Filename: file1 ID = ID100GH Version = 1.00 Size: 251,088 bytes I want it to read the standard output and write the entire line if it matches say ID or Version on separate lines to a text file. Link to comment Share on other sites More sharing options...
0 Squirrelington Posted November 18, 2012 Share Posted November 18, 2012 not sure if this is what you're looking for and my vb.net is a little rusty (and i hate the syntax) but this might help. Dim TestString As String = "Filename: file1" & vbCrLf & "ID = ID100GH" & vbCrLf & "Version = 1.0" & vbCrLf & "Size: 251,088 bytes" Dim PatternString As String = "ID = (.*)" Dim Result As Match = Regex.Match(TestString, PatternString, RegexOptions.None) If (Result.Captures.Count > 0) Then Dim Test As String = Result.Captures(0).Value End If[/CODE] or in C# [CODE] String Content = @"Filename: file1ID = ID100GHVersion = 1.00Size: 251,088 bytes"; String PatternMatcher = @"ID = (.*)"; Match RegexMatcher = Regex.Match(Content, PatternMatcher, RegexOptions.None); if (RegexMatcher.Captures.Count > 0) { String Result = RegexMatcher.Captures[0].Value; }[/CODE] This is an example fyi. Replace TestString with your StdOutput data, etc. Link to comment Share on other sites More sharing options...
0 DPyro Posted November 18, 2012 Author Share Posted November 18, 2012 (edited) Hmm, maybe I could use something like this: Dim value As String = "ID = ID100GH"If value.StartsWith("ID") ThenWriteLineEnd If[/CODE] EDIT: I think I found what I need [CODE]Dim output = New StringBuilder()' Read the lines from FileName into an array of strings. 'Dim input = File.ReadAllLines(FileName)For Each line in input If line.StartsWith("ID") OrElse line.StartsWith("Version") Then output.AppendLine(line) End IfNext[/CODE] Edited November 18, 2012 by DPyro Link to comment Share on other sites More sharing options...
0 DPyro Posted November 18, 2012 Author Share Posted November 18, 2012 (edited) Ok that didn't seem to work :/ This works but now I need to write it to a file: For Each line As String In txtOutputLog.LinesIf line.StartsWith("ID") OrElseline.StartsWith("Version") Then txtOutputLog.Text += line & vbCrLfEnd IfNext[/CODE] Edited November 18, 2012 by DPyro Link to comment Share on other sites More sharing options...
0 DPyro Posted November 20, 2012 Author Share Posted November 20, 2012 For reference, here is my final code: Public out as stringWhile output.HasExited = FalseDim sLine As String = output.StandardOutput.ReadLine If (Not String.IsNullOrEmpty(sLine)) Then out &= sLine & "~"End IfApplication.DoEvents()End While.WaitForExit().Close()[/CODE] [CODE]Dim swriter As StreamWriterDim strOutput As String = (Application.StartupPath & "\" & output & ".txt")For Each line As String In out.Split("~")swriter = File.AppendText(strOutput)If line.StartsWith("ID") OrElse line.StartsWith("Version") Then swriter.WriteLine(line)End Ifswriter.Close()Nextout = ""txtOutputLog.Text = My.Computer.FileSystem.ReadAllText(strOutput) & vbCrLf & "Output file saved to: " & vbCrLf & strOutput[/CODE] Link to comment Share on other sites More sharing options...
Question
DPyro
I am reading the StandardOutput of a process to a string and would like to match certain text and write the full line to a text file.
For example:
Filename: file1
ID = ID100GH
Version = 1.00
Size: 251,088 bytes
I want it to read the standard output and write the entire line if it matches say ID or Version on separate lines to a text file.
Link to comment
Share on other sites
4 answers to this question
Recommended Posts