0

I have lots of txt files and each of the file has below contents. And I want to compare Run with Passed value. e.g. 00001A22 vs 00001A22

I can use excel for those works but I have more than 200 files. It's a big job.

And I try to use powershell to build a script to extract 2 strings and compare it. I tried to select string but I failed. Is there any method to work?

  DRAM Test             Run: 00001A22     Passed: 00001A22 
  Ethernet Test         Run: 000011E2     Passed: 000011E2 
  DRAM Test             Run: 00001BA7     Passed: 00001BA7
  Ethernet Test         Run: 000012EC     Passed: 000012EC 
  DRAM Test             Run: 00001CA3     Passed: 00001CA3 
  Ethernet Test         Run: 00001399     Passed: 00001399 
1
  • what does 00001A22 vs 00001A22 means? Are you trying to search a string inside a file? Commented Mar 24, 2016 at 8:50

2 Answers 2

2

I would use a regex pattern with named capture groups for this:

# Define pattern that captures the hex strings
$CaptureTestPattern = 'Run: (?<run>[0-9A-F]{8}).*Passed: (?<passed>[0-9A-F]{8})'

# Go through each line in file
Get-Content tests.txt |ForEach-Object {
    # Check if string contains both a "run" and "passed" result
    if(($m = [regex]::Match($_,$CaptureTestPattern)).Success)
    {
        # Compare them
        if($m.Groups['run'].Value -eq $m.Groups['passed'])
        {
            # they are the same!
        }
        else
        {
            # they are NOT the same!
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I use a Regex too, with the match operator, your code is more readable than my one line.
OP wanted a oneliner tho, still i think this one is better
He can wrap the above in a function and make his own "one-liner" ;) Technically I'm relying on a single pipeline, I'd argue that counts as a one-liner
0

According the fact the file test.txt contains the following :

DRAM Test Run: 00001A22 Passed: 00001A22
Ethernet Test Run: 000011E2 Passed: 000011E2
DRAM Test Run: 00001BA7 Passed: 00001BA7
Ethernet Test Run: 000012EC Passed: 000012ED
DRAM Test Run: 00001CA3 Passed: 00001CA3
Ethernet Test Run: 00001399 Passed: 00001399

You can use :

Get-Content D:\Temp\test.txt | % {$n=0}{$_ -match '^.*Run: (.*) Passed: (.*)$' | Out-Null; if ($Matches[1] -ne $Matches[2]){"line $n KO"}; $n++}

it returns

line 3 KO

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.