KAPOW

Welcome to the Kapow forum. Here you can get help, use your skills to help others and enjoy hanging out in the company of other Kapow Robot Developers.


[solved]How to parse the sdout of CommandLine

Share

kennyf

Posts : 6
Points : 68
Join date : 2018-10-23

[solved]How to parse the sdout of CommandLine

Post by kennyf on Fri Nov 30, 2018 2:13 pm

Hi, I'm using the Execute Command Line step to do a simple 'PING 127.0.0.1' command.

I extracted the Stdout to a Long Text variable.

Since I want to calculate the average ping time (= xx ms) from the four lines in the response, how best to do this?
Surely if i use convertors there'll be too many matches?

There are Line Feed (LF) characters in the variable...Is there a way to look through each 'line' of the output and parse each line one-by-one?

I tried saving it as a .txt file but unlike an EXCEL file, there doesn't seem to be a way to loop through each line in a text file.

Thanks in advance!
avatar
jking

Posts : 57
Points : 1829
Join date : 2014-03-01
Location : USA

Re: [solved]How to parse the sdout of CommandLine

Post by jking on Fri Nov 30, 2018 8:33 pm

Without seeing an example of your output, I would extract the text to a long text variable, then I would use a Regular Expres​sion(using Replace Text in Kapow) to pattern-out the text needed to an Integer variable.  

Here is an example of a Ping and the Response Text with patterns to extract the time in milliseconds:

Pinging www.google.com [74.125.224.82] with 1500 bytes of data:
Reply from 74.125.224.82: bytes=1500 time=68ms TTL=52
Reply from 74.125.224.82: bytes=1500 time=68ms TTL=52
Reply from 74.125.224.82: bytes=1500 time=65ms TTL=52
Reply from 74.125.224.82: bytes=1500 time=66ms TTL=52
Reply from 74.125.224.82: bytes=1500 time=70ms TTL=52
Ping statistics for 74.125.224.82:
   Packets: Sent = 5, Received = 5, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
   Minimum = 65ms, Maximum = 70ms, Average = 67ms

Pattern 1 for Minimum:
.*(Minimum\s\=\s(\d\d)).*  
Read as Ignore everything up and including text string "Minimum" followed by a space, followed by an "=" sign, followed by a space.  Then, return the next 2 digits, and ignore all subsequent characters.
Replace Expression: $2
Result: 65

Pattern 2 for Maximum:
.*(Maximum\s\=\s(\d\d)).*
Read as Ignore everything up and including text string "Maximum" followed by a space, followed by an "=" sign, followed by a space.  Then, return the next 2 digits, and ignore all subsequent characters.
Replace Expression: $2
Result: 70

Pattern 3 for Average:
.*(Average\s\=\s(\d\d)).*
Read as Ignore everything up and including text string "Average" followed by a space, followed by an "=" sign, followed by a space.  Then, return the next 2 digits, and ignore all subsequent characters.
Replace Expression: $2
Result: 67

hth

kennyf

Posts : 6
Points : 68
Join date : 2018-10-23

Re: [solved]How to parse the sdout of CommandLine

Post by kennyf on Mon Dec 03, 2018 8:23 am

Hi jking,

Yes, this was my approach too. Thank you for the response. My only concern is that the long text might get very long and I wonder if for future examples, I might output a terminal session to a gigantic text file and if using convertors and regex like this will actually slow the robot down.

Anyways, this is good for my current situation. I was just hoping for a way to read a text file line-by-line (apply regex to each line) as how it is done using traditional .NET or JAVA programming.

Cheers!
avatar
pavel.vraj

Posts : 86
Points : 866
Join date : 2016-11-04
Location : Prague, Czech Republic

Re: [solved]How to parse the sdout of CommandLine

Post by pavel.vraj on Mon Dec 03, 2018 3:55 pm

Hi,
looping via lines in variable is very simple, create a "For Each Text Part" step, where you select an input variable, delimiter (there you type "\n") and output variable, where you'll have each line during looping and then you can do anything you need with each line.
BR, Pavel Vraj

kennyf

Posts : 6
Points : 68
Join date : 2018-10-23

Re: [solved]How to parse the sdout of CommandLine

Post by kennyf on Mon Dec 03, 2018 5:04 pm

Yes! This is precisely what I am looking for! The delimiter method works...

Thanks Pavel!

Sponsored content

Re: [solved]How to parse the sdout of CommandLine

Post by Sponsored content


    Current date/time is Tue Dec 18, 2018 11:10 pm