Regex - find certain word that appear multiple times

I have searched online and in this forum but can’t seem to find the correct solution to my peculiar problem.

So, I am interacting with a server via SSH terminal connectio and using GetText activity to read the screen. I send a command like "&& echo “pass” || echo “fail” to check if a file was found. If this returns pass, then it’s the line immediately after that command. If returns fail, then there is a line like "-Bash: [: missing ‘]’ before fail is displayed.
How do I read this value returned to determine it if’s a pass or fail?

IN ADDITION:

this sort of command is sent twice during this interaction. How will I get the value of the second pass/fail check since there will now be multiple “echo” commands on that screen?

I hope my question is clear. Any help is appreciated :).

@Rohi2023

You have invoke power shell activity to do these…you need not use ui activities

Cheers

I am using terminal activities and GetText to read the screen into a string. I am not using powershell for the connection.

Why do I need to use powershell as opposed to the terminal activities?

@Rohi2023

Oops mybad…I got the question totallywrong

Can you sharethe output of get text and what value you need using regex

Cheers

Thanks.
Below is a snippet from the file. I am trying to get the value that comes after the echo (pass or fail). The first part is what I get when it fails, the second part is what I get when it passes.

␉[company - Pre-Prod] [xxxxx@xxxxxx ~] > cd folder_scripts

␉[company - Pre-Prod] [xxxxx@xxxxxx folder_scripts] ] > cd test_prop_files

<mv /home/automation/27042023_123453_data.properties ./

mv: cannot stat ␉/home/automation/27042023_123453_data.properties: No such file or directory

[company - Pre-Prod] [xxxxx@xxxxxx test_prop_files] > cd …/…

<27042023_123453_data.properties] && echo “pass” || echo “fail”

-bash: [: missing `]’

fail

[company - Pre-Prod] [xxxxx@xxxxxx folder_scripts] >

SECOND PART - when it passes:

␉[company - Pre-Prod] [xxxxx@xxxxxx ~] > cd folder_scripts

␉[company - Pre-Prod] [xxxxx@xxxxxx folder_scripts] ] > cd test_prop_files

<mv /home/automation/27042023_123453_data.properties ./

mv: cannot stat ␉/home/automation/27042023_123453_data.properties: No such file or directory

[company - Pre-Prod] [xxxxx@xxxxxx test_prop_files] > cd …/…

<27042023_123453_data.properties] && echo “pass” || echo “fail”
pass

[company - Pre-Prod] [xxxxx@xxxxxx folder_scripts] >

I want to read the result of the echo - whether it is pass or fail.

Also this echo command is sent twice. What I have noticed is that by the time it is sent the second time, the previous echo command and the result has disappeared from the screen, so only the latest echo and result are grabbed in the second “GetText” activity.

Update:

What I have noticed is that by the time it is sent the second time, the previous echo command and the result has disappeared from the screen, so only the latest echo and result are grabbed in the second “GetText” activity.

I increased the size of the terminal screen and now I am getting the entire text that means I will have 2 “echo” results in my second GetText activity.

Just to give more context on what I am currently doing:
I am using the matches activity to look for "pass " (notice space after the word. this is to ensure it’s not matching with the “pass” inside the echo command. I check the IEnumerable.Count and if this is 0, it means the result is fail. This doesn’t look like an elegant solution though, and might fail on the second echo command?

1 Like

Hi

I believe this approach is indeed a good one

First let’s get the text between the word echo and company between which we can get to know whether it is pass or fail

Use a assign activity like this

stroutput = System.Text.RegularExpressions.Regex.Match(str_input.tostring,”(?<=echo)[\S\s]*(?=\Wcompany|company)”)

This will give the text between those words

Now if that string obtained contain word as pass then it’s fine or it will fail
Use a if activity with condition before

stroutput.ToString.Contains(“pass”)
If true means bot goes to then block where u can understand that it has passed the script execution
If fails it goes to else block

Cheers @Rohi2023

Hi thanks, but this is not working because it returns the pass in && echo “pass” || echo “fail” so even when it fails, it the condition evaluates to true. It’s even worse when the second echo command is sent and multiple “pass” now appears on screen (although both commands actually returned ‘fail’).

Hi,

How about the following expression?

System.Text.RegularExpressions.Regex.IsMatch(yourString,"(?<=&&\s+echo\s+\Wpass\W\s+\|\| echo\s+\Wfail\W\s*)pass")

Sample

Sample20230428-1L.zip (2.5 KB)

Regards,

Thanks, this works for my first scenario! In the case where the second "echo command also runs?

␉[company - Pre-Prod] [xxxxx@xxxxxx ~] > cd folder_scripts

␉[company - Pre-Prod] [xxxxx@xxxxxx folder_scripts] ] > cd test_prop_files

<mv /home/automation/27042023_123453_data.properties ./

mv: cannot stat ␉/home/automation/27042023_123453_data.properties: No such file or directory

[company - Pre-Prod] [xxxxx@xxxxxx test_prop_files] > cd …/…

<27042023_123453_data.properties] && echo “pass” || echo “fail”

-bash: [: missing `]’

fail

[company - Pre-Prod] [xxxxx@xxxxxx folder_scripts] >
— some more commands, then call “echo” again —
<27042023_123453_data.properties] && echo “pass” || echo “fail”
pass (or fail)

There are a couple of scenarios that could happen here:

  1. 1st echo fail, second pass
  2. 1st echo pass, second fail
  3. both echo pass
  4. both echo fail
    5…

How will I be able to get each result independently ?

Hi,

Can you try the following?

Sample20230502-1L.zip (2.8 KB)

mc = System.Text.RegularExpressions.Regex.Matches(yourString,"&&\s+echo\s+\Wpass\W\s+\|\| echo\s+\Wfail\W[\s\S]*?(?<RESULT>pass|fail)")

Then

currentItem.Groups("RESULT").Value returns “pass” or “fail” and we can easily branch logic using IF activity.

Regards,

This worked like a charm! Thanks so much @Yoichi, you are a :star:. I’m very new to UiPath and hope to be as good as you are someday :smile:.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.