confusing output from process

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm using a Process object to run an external command line program that gathers cpu utilization stats. I get the output using "process.StandardOutput.ReadToEnd()" and assign it to a string variable "stats". All of that works great. The problem is when I go to parse and display the output from the program

Here's a snippet of the C# code I'm using to display the output
---------------------------------------------------
string stats = process.StandardOutput.ReadToEnd()
string[] lines = stats.Split('\n')

Console.WriteLine(stats)

for (int i=0; i < lines.Length; i++)
Console.WriteLine("Line {0}: |{1}|", i.ToString(), lines)

---------------------------------------------------

And here's the output when the code is run
---------------------------------------------------
Variable = host.hrDevice.hrProcessorTable.hrProcessorEntry.hrProcessorLoad.
Value = Integer32 3

Variable = host.hrDevice.hrProcessorTable.hrProcessorEntry.hrProcessorLoad.
Value = Integer32

|ine 0: |Variable = host.hrDevice.hrProcessorTable.hrProcessorEntry.hrProcessorLoad.
|ine 1: |Value = Integer32 3
|ine 2:
|ine 3: |Variable = host.hrDevice.hrProcessorTable.hrProcessorEntry.hrProcessorLoad.
|ine 4: |Value = Integer32
|ine 5:
Line 6: |
 
Paul said:
I'm using a Process object to run an external command line program that gathers cpu utilization stats. I get the output using "process.StandardOutput.ReadToEnd()" and assign it to a string variable "stats". All of that works great. The problem is when I go to parse and display the output from the program.

Here's a snippet of the C# code I'm using to display the output:
----------------------------------------------------
string stats = process.StandardOutput.ReadToEnd();
string[] lines = stats.Split('\n');

Console.WriteLine(stats);

for (int i=0; i < lines.Length; i++) {
Console.WriteLine("Line {0}: |{1}|", i.ToString(), lines);
}
----------------------------------------------------


And here's the output when the code is run:
----------------------------------------------------
Variable = host.hrDevice.hrProcessorTable.hrProcessorEntry.hrProcessorLoad.1
Value = Integer32 38

Variable = host.hrDevice.hrProcessorTable.hrProcessorEntry.hrProcessorLoad.2
Value = Integer32 3


|ine 0: |Variable = host.hrDevice.hrProcessorTable.hrProcessorEntry.hrProcessorLoad.1
|ine 1: |Value = Integer32 38
|ine 2: |
|ine 3: |Variable = host.hrDevice.hrProcessorTable.hrProcessorEntry.hrProcessorLoad.2
|ine 4: |Value = Integer32 3
|ine 5: |
Line 6: ||


The string you get from StandardOutput.ReadToEnd() has "\r\n" at the end
of each line. When you split on "\n" the "\r" is still there and causes
the second "|" character to print in column 0 (or column 1 depending on
how you count).

You'll want to do something like lines.TrimEnd( '\r') in the
WriteLine() call.
 
The process is outputing carriage return/line feed combos. How you then process
this output is entirely up to you, since different programs may output crlf
pairs, and
others may reverse this and output lfcr pairs, and others might just output one
or
the other. This is especially prevalent when talking with Macs over a network
or
using some internet based protocols.


--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

Paul O said:
I'm using a Process object to run an external command line program that
gathers cpu utilization stats. I get the output using
"process.StandardOutput.ReadToEnd()" and assign it to a string variable "stats".
All of that works great. The problem is when I go to parse and display the
output from the program.
Here's a snippet of the C# code I'm using to display the output:
----------------------------------------------------
string stats = process.StandardOutput.ReadToEnd();
string[] lines = stats.Split('\n');

Console.WriteLine(stats);

for (int i=0; i < lines.Length; i++) {
Console.WriteLine("Line {0}: |{1}|", i.ToString(), lines);
}
----------------------------------------------------


And here's the output when the code is run:
----------------------------------------------------
Variable = host.hrDevice.hrProcessorTable.hrProcessorEntry.hrProcessorLoad.1
Value = Integer32 38

Variable = host.hrDevice.hrProcessorTable.hrProcessorEntry.hrProcessorLoad.2
Value = Integer32 3


|ine 0: |Variable = host.hrDevice.hrProcessorTable.hrProcessorEntry.hrProcessorLoad.1
|ine 1: |Value = Integer32 38
|ine 2: |
|ine 3: |Variable = host.hrDevice.hrProcessorTable.hrProcessorEntry.hrProcessorLoad.2
|ine 4: |Value = Integer32 3
|ine 5: |
Line 6: ||

bars aren't displaying in the proper location? If I replace the | with any
other character I get the same result. I think there may be something in the
original string that's causing a problem, but I can't seem to figure out what.
 
Paul,

Chances are that while reading the lines, there is a linefeed and a new
line combo, so when you get the lines, they are not properly delimited.
Instead of calling ReadToEnd, you should cycle through a loop while ReadLine
(on the StandardOutput) doesn't return null. You can add your strings into
an ArrayList, and then cycle through the elements in the array list to
process them.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Paul O said:
I'm using a Process object to run an external command line program that
gathers cpu utilization stats. I get the output using
"process.StandardOutput.ReadToEnd()" and assign it to a string variable
"stats". All of that works great. The problem is when I go to parse and
display the output from the program.
Here's a snippet of the C# code I'm using to display the output:
----------------------------------------------------
string stats = process.StandardOutput.ReadToEnd();
string[] lines = stats.Split('\n');

Console.WriteLine(stats);

for (int i=0; i < lines.Length; i++) {
Console.WriteLine("Line {0}: |{1}|", i.ToString(), lines);
}
----------------------------------------------------


And here's the output when the code is run:
----------------------------------------------------
Variable = host.hrDevice.hrProcessorTable.hrProcessorEntry.hrProcessorLoad.1
Value = Integer32 38

Variable = host.hrDevice.hrProcessorTable.hrProcessorEntry.hrProcessorLoad.2
Value = Integer32 3


|ine 0: |Variable = host.hrDevice.hrProcessorTable.hrProcessorEntry.hrProcessorLoad.1
|ine 1: |Value = Integer32 38
|ine 2: |
|ine 3: |Variable = host.hrDevice.hrProcessorTable.hrProcessorEntry.hrProcessorLoad.2
|ine 4: |Value = Integer32 3
|ine 5: |
Line 6: ||

vertical bars aren't displaying in the proper location? If I replace the |
with any other character I get the same result. I think there may be
something in the original string that's causing a problem, but I can't seem
to figure out what.
 
Thanks, that worked great! When I read from ReadLine directly like that it even filters out whatever crlf combo was at the end of the lines

Thanks everyone

Pau

----- Nicholas Paldino [.NET/C# MVP] wrote: ----

Paul

Chances are that while reading the lines, there is a linefeed and a ne
line combo, so when you get the lines, they are not properly delimited
Instead of calling ReadToEnd, you should cycle through a loop while ReadLin
(on the StandardOutput) doesn't return null. You can add your strings int
an ArrayList, and then cycle through the elements in the array list t
process them

Hope this helps


--
- Nicholas Paldino [.NET/C# MVP
- (e-mail address removed)

Paul O said:
I'm using a Process object to run an external command line program tha
gathers cpu utilization stats. I get the output usin
"process.StandardOutput.ReadToEnd()" and assign it to a string variabl
"stats". All of that works great. The problem is when I go to parse an
display the output from the program
Here's a snippet of the C# code I'm using to display the output
---------------------------------------------------
string stats = process.StandardOutput.ReadToEnd()
string[] lines = stats.Split('\n')
Console.WriteLine(stats)
for (int i=0; i < lines.Length; i++)
Console.WriteLine("Line {0}: |{1}|", i.ToString(), lines)

---------------------------------------------------
---------------------------------------------------
Variable
host.hrDevice.hrProcessorTable.hrProcessorEntry.hrProcessorLoad.
Value = Integer32 3
host.hrDevice.hrProcessorTable.hrProcessorEntry.hrProcessorLoad.
Value = Integer32 host.hrDevice.hrProcessorTable.hrProcessorEntry.hrProcessorLoad.
|ine 1: |Value = Integer32 3
|ine 2:
|ine 3: |Variable host.hrDevice.hrProcessorTable.hrProcessorEntry.hrProcessorLoad.
|ine 4: |Value = Integer32
|ine 5:
Line 6: |
---------------------------------------------------
Can anyone explain why the beginning of the lines are cut off and th

vertical bars aren't displaying in the proper location? If I replace the
with any other character I get the same result. I think there may b
something in the original string that's causing a problem, but I can't see
to figure out what
 
Back
Top