narmi2
asked on
Commandline output to Windows Program
Dear Experts,
I have a command line program which when run, outputs something like this from cmd:
version;3
active;1
interface;eth0
nick;eth0
created;1224363832
updated;1224407701
totalrx;622
totaltx;198
currx;24485590
curtx;1987530
...
and it goes on
How do I capture the value after "totalrx" and display it in a textbox?
Please help.
I have a command line program which when run, outputs something like this from cmd:
version;3
active;1
interface;eth0
nick;eth0
created;1224363832
updated;1224407701
totalrx;622
totaltx;198
currx;24485590
curtx;1987530
...
and it goes on
How do I capture the value after "totalrx" and display it in a textbox?
Please help.
ASKER
Is this code reading text from a file?
ASKER
so far I have this which captures everything and sticks it into a textbox. I don't know how to display totalrx in the textbox alone?
protected virtual void OnButton2Clicked (object sender, System.EventArgs e)
{
Process p = new Process();
p.StartInfo.UseShellExecut e = false;
p.StartInfo.RedirectStanda rdOutput = true;
p.StartInfo.FileName = "the commandline program to run goes here";
p.Start();
string output = p.StandardOutput.ReadToEnd ();
p.WaitForExit();
entry1.Text = output";
}
protected virtual void OnButton2Clicked (object sender, System.EventArgs e)
{
Process p = new Process();
p.StartInfo.UseShellExecut
p.StartInfo.RedirectStanda
p.StartInfo.FileName = "the commandline program to run goes here";
p.Start();
string output = p.StandardOutput.ReadToEnd
p.WaitForExit();
entry1.Text = output";
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hi narmi2;
The meaning of the Regex pattern, "(?i)totalrx;(.*?)\r", is as follows:
oups[1].Va lue;, the Groups[1] tells Regex to return Group 1 the only group in this pattern and Value returns the string of the group.
Fernando
The meaning of the Regex pattern, "(?i)totalrx;(.*?)\r", is as follows:
- (?i) - Tells Regex that the pattern that follows should be evaluated with case insensitive matching.
- totalrx; - Look for this sequence of characters in the string.
- (.*?) - Group the next sequence of characters until the first character after the end group, ).
- \r - This is a return character and tells to group to stop grouping, in this case.
Fernando
ASKER
For some reason the regex is not returning anything?
I tried this, but it's output was blank:
Console.WriteLine (Regex.Match(output, @"(?i)totalrx;(.*?)\r").Gr oups[1].Va lue);
If I do the following, it displays text like the text I posted in the original post:
Console.WriteLine (output);
I tried this, but it's output was blank:
Console.WriteLine (Regex.Match(output, @"(?i)totalrx;(.*?)\r").Gr
If I do the following, it displays text like the text I posted in the original post:
Console.WriteLine (output);
Please post your code. I have tested this solution before posting.
ASKER
Here's the code:
using System;
using System.Diagnostics;
using System.Text.RegularExpressions;
namespace test_console
{
class MainClass
{
public static void Main(string[] args)
{
ProcessStartInfo psi = new ProcessStartInfo ();
psi.FileName = "command to execute goes here";
psi.Arguments = "optional argument goes here";
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
Process p = Process.Start (psi);
string ret = p.StandardOutput.ReadToEnd ();
p.WaitForExit ();
Console.WriteLine (Regex.Match(output, @"(?i)totalrx;(.*?)\r").Groups[1].Value);
}
}
}
ASKER
Please ignore the copy/past error, the line:
Console.WriteLine (Regex.Match(output, @"(?i)totalrx;(.*?)\r").Gr oups[1].Va lue);
Should have been:
Console.WriteLine (Regex.Match(ret, @"(?i)totalrx;(.*?)\r").Gr oups[1].Va lue);
Console.WriteLine (Regex.Match(output, @"(?i)totalrx;(.*?)\r").Gr
Should have been:
Console.WriteLine (Regex.Match(ret, @"(?i)totalrx;(.*?)\r").Gr
ASKER
Figured it out. I am coding for Linux so instead of /r I hade to use /n
Thanks for all the help.
Thanks for all the help.
Yes you are correct files created on Unix type machine do use different line terminators.
Not a problem, glad I was able to help. ;=)
Not a problem, glad I was able to help. ;=)
StreamReader lo_reader = new StreamReader(@#FILE#);
lo_reader.open();
while(!lo_reader.EndOfStre
{
string ls_input = lo_reader.ReadLine();
if(ls_input.Contains("tota
{
ls_input = ls_input.Replace("totalrx;
textbox1.Text = ls_input
}
}
lo_reader.close();
if you want to display the totalrx before it just remove the replace line. and remember to include the
#include System.IO;
at the top.
have done it quite quickly so you'll have to substitue your names in and beware of caps I might have missed.