0

I have a list of Contract numbers in a text file.

185
166
504
506
507
510
509

I have servername like SERVER999AUTO1. I need to check if the number 999 present in the file. if present then, no operation else we have to perform certain operation.

I tried below, but for each contract it is printing the else value

$file = Get-Content "C:\list.txt"
$containsWord = $file | %{$_ -match "SERVER999AUTO1"}
if ($containsWord -contains $true) {
    Write-Host "There is!"
} else {
    Write-Host "There ins't!"
}

Please let me know on this.

3
  • 1
    Your file is a List of numbers however your trying to match the word SERVER999AUTO1, where does that come from? Commented Mar 17, 2022 at 12:53
  • @SantiagoSquarzon: If you see the number in the string 999, I have to match that if that is part of the file or not. Commented Mar 17, 2022 at 12:58
  • Besides the value actually not being in your .txt file, you're attempting to match the current number to "SERVER999AUTO1", which it won't match it. In other words, your precedence is wrong when it's supposed to be: "SERVER999AUTO1" -match $_. Change it to that and it should work. Commented Mar 17, 2022 at 13:03

2 Answers 2

3

Although a bit unclear, you can parse the value from the servername and look for it in the text file like so:

$serverName = 'SERVER999AUTO1'
$valueISeek = ([regex]'(?i)server(\d+).*').Match($serverName).Groups[1].Value

if ((Get-Content "C:\list.txt") -contains $valueISeek) {
    Write-Host "Value '$valueISeek' found!" -ForegroundColor Green
} else {
    Write-Host "Value '$valueISeek' could not be found!" -ForegroundColor Red
}

Regex details:

(?i)         Match the remainder of the regex with the options: case insensitive (i)
server       Match the characters “server” literally
(            Match the regular expression below and capture its match into backreference number 1
   \d        Match a single digit 0..9
      +      Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)           
.            Match any single character that is not a line break character
   *         Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the response. what changes I have to have if the starting the name doesn't have SERVER. e.g. A92SW999AUTO1AP/B97SW999AUTO1AP
@EmptyCoder, if the number of interest is always a 3-digit sequence, you can use the following: $valueISeek = $serverName -replace '.+(\d{3}).+', '$1'
0

Continuing from my comment...

The reason why your else block is activated is because, your current iteration of the number you're on itself does not match "SERVER999AUTO1". You need to switch the order as "SERVER999AUTO1" matches "999", not the other way around:

$file = Get-Content "C:\list.txt"
$containsWord = $file | %{"SERVER999AUTO1" -match $_}
if ($containsWord -contains $true) {
    Write-Host "There is!"
} else {
    Write-Host "There ins't!"
}

Reversing the order to the correct order will work. In this case, I personally would use a switch statement as it can perform multiple conditions with a "cleaner" look:

$toMatch = "SERVER999AUTO1"
switch -File ("C:\list.txt")
{
    {$toMatch -match $_} { "There is!"; Break }
    default {"There isn't" }
}

... then again, it's all just preference.

2 Comments

it is fine but I want the result to be printed once. not multiple time
@EmptyCoder, good thing it only does it once:) hence the Break.

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.