Find lines in one Object based on strings in other object - powershell

Finding lines in one file based on rows in another file.
I have one object $A with some rows like:
0c7d3283-bec2-4db1-9078-ebb79d21afdf
200bc957-26dd-4e8e-aa6e-00dc357c4ac2
218e0d2a-0e8b-4a68-8136-8f5dd749a614
I want to find matches in object $B for those rows and print the lines with the matches to an output file.
I've been trying for a week now (my first week in powershell :) )
I've come to:
$F = $B | ForEach-Object{ $A | Select-String -Pattern $_$ -AllMatches| Select-Object line }
but this doesn't give me any returning results.
Who is willing to help me out?

If you want to match your first Array, with something that should match part of a string in a second array you do something like the code below:
$A = #("0c7d3283-bec2-4db1-9078-ebb79d21afdf", "200bc957-26dd-4e8e-aa6e-00dc357c4ac2", "218e0d2a-0e8b-4a68-8136-8f5dd749a614")
$B = #("Something 0c7d3283-bec2-4db1-9078-ebb79d21afdf", "Something else 200bc957-26dd-4e8e-aa6e-00dc357c4ac2", "Something also e3df3978-beb7-4545-bc48-ff40d8453be1")
foreach ($Line in $A) {
if($B -match $Line) {
$B | Where-Object {$_ -match $Line}
}
}
We first loop through all the lines in the first object, then compare if the line is matched to anything in the second array. If we find a match we go through Array B to find where the Line from A matches.
You could make this code a hell lot prettier, but this the most understandable way I can write it.
Old Answer
You could use the Compare-Object cmdlet to compare the two arrays, then use the -IncludeEqual switch to show where there are matches and then use the -ExcludeDifferent switch to remove the results that do not match.
Then take that Output and put in in a file. A simple test could be something like this:
$A = #("0c7d3283-bec2-4db1-9078-ebb79d21afdf", "200bc957-26dd-4e8e-aa6e-00dc357c4ac2", "218e0d2a-0e8b-4a68-8136-8f5dd749a614")
$B = #("0c7d3283-bec2-4db1-9078-ebb79d21afdf", "200bc957-26dd-4e8e-aa6e-00dc357c4ac2", "e3df3978-beb7-4545-bc48-ff40d8453be1")
(Compare-Object -ReferenceObject $A -DifferenceObject $B -ExcludeDifferent -IncludeEqual).InputObject | Out-File .\output.txt
This should output a file in your Shells current working directory with the two GUIDs that match:
0c7d3283-bec2-4db1-9078-ebb79d21afdf
200bc957-26dd-4e8e-aa6e-00dc357c4ac2
Where the one that didn't match is not included.

Related

Powershell comparing values / then checking the validity

I have been trying numerous functions in Powershell including Comparison Object, for each function but with no success.
This is what I want to do.
I have two files named as File 1 and File 2. Within File 1, I have a line like this.
In File 2, I have this.
So I want to compare the value of ContractGroups from File 1 to the value of ContractGroups in file 2. If matched, I want to add an outcome to the file.
The difficulty I am having is that I can set up a variable to get the content for matching. But when comparing, I want to only take into account of ContractGroups for a match. So as soon as file 2 is scanned, then I want to see if the value of ContractGroups matches to file 1.
I have tried this.
$file1 = Get-Content "C:\Users\Altunokc\Desktop\Guardian\NotUsed\20200513 Environment parameters\EnvironmentParameters_ICE_EU_PROD.xml"
$file2 = Get-Content "C:\Users\Altunokc\Desktop\inetpub\wwwroot\EU\Guardian\Website\appSettings.config"
$pattern = $file1| %{$_ -MATCH '.Name="ContractGroups" Value'}
$result = "C:\Users\Altunokc\Desktop\Differentdd.txt"
$file1 | foreach { $pattern = $file2 -match $_
if ( $match ) { $match | Out-File -Force $result -Append }
}
and this way
if(Compare-Object -ReferenceObject $(Get-Content $file2 ) -DifferenceObject $(Get-Content $file1 ) | %{$_ -MATCH '.Name="ContractGroups" Value'} -SimpleMatch|
Out-File -FilePath C:\Users\Altunokc\Desktop\Different.txt)
{"match"}
Else {"No "}
No luck.
I think for comparison, I just need to find a way of scanning the line that contains Name="ContractGroups" Value from file 1 and then reading its value (CC) and then scanning file 2 to first see if has a line matches Name="ContractGroups" Value and if yes, and then does the value of it is the same (CC) as file one? If yes, then just write that "Matched" to a blank file.
It would be helpful if you shed light on this. Sorry as I am new on PS.

Powershell text search - multiple matches

I have a group of .txt files that contain one or two of the following strings.
"red", "blue", "green", "orange", "purple", .... many more (50+) possibilities in the list.
If it helps, I can tell if the .txt file contains one or two items, but don't know which one/ones they are. The string patterns are always on their own line.
I'd like the script to tell me specifically which one or two string matches (from the master list) it found, and the order in which it found them. (Which one was first)
Since I have a lot of text files to search, I'd like to write the output results to a CSV file as I search.
FILENAME1,first_match,second_match
file1.txt,blue,red
file2.txt,red, blue
file3.txt,orange,
file4.txt,purple,red
file5.txt,purple,
...
I've tried using many individual Select-Strings returning Boolean results to set variables with any matches found, but with the number of possible strings it gets ugly real fast. My search results for this issue has provided me with no new ideas to try. (I'm sure I'm not asking in the correct way)
Do I need to loop through each line of text in each file?
Am I stuck with the process of elimination method by checking for the existence of each search string?
I'm looking for a more elegant approach to this problem. (if one exists)
Not very intuïtive but elegant...
Following switch statement
$regex = "(purple|blue|red)"
Get-ChildItem $env:TEMP\test\*.txt | Foreach-Object{
$result = $_.FullName
switch -Regex -File $_
{
$regex {$result = "$($result),$($matches[1])"}
}
$result
}
returns
C:\Users\Lieven Keersmaekers\AppData\Local\Temp\test\file1.txt,blue,red
C:\Users\Lieven Keersmaekers\AppData\Local\Temp\test\file2.txt,red,blue
where
file1 contains first blue, then red
file2 contains first red, then blue
You can use regex to search to get index (startpos. in line) combine with Select-String which returns linenumber and you're good to go.
Select-String supports an array as value for -Pattern, but unfortunately it stops on a line after first match even when you use -AllMatches (bug?). Because of this we have to search one time per word/pattern. Try:
#List of words. Had to escape them because Select-String doesn't return Matches-objects (with Index/location) for SimpleMatch
$words = "purple","blue","red" | ForEach-Object { [regex]::Escape($_) }
#Can also use a list with word/sentence per line using $words = Get-Content patterns.txt | % { [regex]::Escape($_.Trim()) }
#Get all files to search
Get-ChildItem -Filter "test.txt" -Recurse | Foreach-Object {
#Has to loop words because Select-String -Pattern "blue","red" won't return match for both pattern. It stops on a line after first match
foreach ($word in $words) {
$_ | Select-String -Pattern $word |
#Select the properties we care about
Select-Object Path, Line, Pattern, LineNumber, #{n="Index";e={$_.Matches[0].Index}}
}
} |
#Sort by File (to keep file-matches together), then LineNumber and Index to get the order of matches
Sort-Object Path, LineNumber, Index |
Export-Csv -NoTypeInformation -Path Results.csv -Encoding UTF8
Results.csv
"Path","Line","Pattern","LineNumber","Index"
"C:\Users\frode\Downloads\test.txt","file1.txt,blue,red","blue","3","10"
"C:\Users\frode\Downloads\test.txt","file1.txt,blue,red","red","3","15"
"C:\Users\frode\Downloads\test.txt","file2.txt,red, blue","red","4","10"
"C:\Users\frode\Downloads\test.txt","file2.txt,red, blue","blue","4","15"
"C:\Users\frode\Downloads\test.txt","file4.txt,purple,red","purple","6","10"
"C:\Users\frode\Downloads\test.txt","file4.txt,purple,red","red","6","17"
"C:\Users\frode\Downloads\test.txt","file5.txt,purple,","purple","7","10"

Retrieving second part of a line when first part matches exactly

I used the below steps to retrieve a string from file
$variable = 'abc#yahoo.com'
$test = $variable.split('#')[0];
$file = Get-Content C:\Temp\file1.txt | Where-Object { $_.Contains($test) }
$postPipePortion = $file | Foreach-Object {$_.Substring($_.IndexOf("|") + 1)}
This results in all lines that contain $test as a substring. I just want the result to contain only the lines that exactly matches $test.
For example, If a file contains
abc_def|hf#23$
abc|ohgvtre
I just want the text ohgvtre
If I understand the question correctly you probably want to use Import-Csv instead of Get-Content:
Import-Csv 'C:\Temp\file1.txt' -Delimiter '|' -Header 'foo', 'bar' |
Where-Object { $_.foo -eq $test } |
Select-Object -Expand bar
To address the exact matching, you should be testing for equality (-eq) rather than substring (.Contains()). Also, there is no need to parse the data multiple times. Here is your code, rewritten to to operate in one pass over the data using the -split operator.
$variable = 'abc#yahoo.com'
$test = $variable.split('#')[0];
$postPipePortion = (
# Iterate once over the lines in file1.txt
Get-Content C:\Temp\file1.txt | foreach {
# Split the string, keeping both parts in separate variables.
# Note the backslash - the argument to the -split operator is a regex
$first, $second = ($_ -split '\|')
# When the first half matches, output the second half.
if ($first -eq $test) {
$second
}
}
)

Compare 2 arrays with wildcards in powershell

Say we have 2 .txt-files like so:
users.txt
user.name1
user.name2
admin.name1
admin.name2
shares.txt
\\someserver\somefolder\user.name1
\\someserver\somefolder\user.name2
\\someserver\someotherfolder\admin.name1
\\someotherserver\somefolder\admin.name2
\\someplaceelse\somefolder\no.name
I want to compare these 2 points of data to show only the rows in "shares.txt" that match a name in "users.txt". I figure we need to use some sort of wildcardsearch as the username is always included in the uncpath but will not be exactly the same, here is an example of what i have tried so far without success
$users = Get-Content ".\users.txt"
$shares = Get-Content ".\shares.txt"
foreach ($line in $users)
{
if ("*$line*" -like "$shares")
}
Write-Host "this line matches $line"
}
}
foreach ($line in $users)
{
if ($shares -like "*$line*")
{
Write-Host "$($shares | where {$_ -like "*$line*"}) this line matches $line"
}
}
to get the intended information the current value of the loop must be included in the search.
The problem in your code was that like operator only allows wild cards on the right side of the expression. So the matching must happen the other way around.

Powershell foreach over two arrays

All,
Trying to get this working but have not had any luck with ps.
$a =
"install.res.1028.dll"
"install.res.1031.dll"
"install.res.1033.dll"
"install.res.1036.dll"
"install.res.1040.dll"
"install.res.1041.dll"
"install.res.1042.dll"
"install.res.2052.dll"
$b =
"install.res.1041.dll"
"install.res.1042.dll"
"install.res.2052.dll"
I just wish to have a new array with the values that are found in $a and not found in $b, trying to test it out with write-host but no luck.
I have tried compare-object but I am unable to pull out just the name. I am a totally noob with ps.
Please, any suggestions appreciated.
foreach ($i in $a)
{ foreach-object ($b | where { {$_.name} -ne $i }) { write-host $i}}
Another solution using compare-object as follows:
$c = Compare-Object -ReferenceObject $a -DifferenceObject $b -PassThru
output:
install.res.1028.dll
install.res.1031.dll
install.res.1033.dll
install.res.1036.dll
install.res.1040.dll
CORRECTION:
The above code WILL work for the case where DifferenceObject is guaranteed to be a subset of ReferenceObject. It will FAIL, though, if there are additional objects in DifferenceObject that are not also present in ReferenceObject. The above code returns any objects which are present in EITHER ReferenceObject OR DifferenceObject but NOT in both.
To properly return ONLY objects in ReferenceObject that are not also present in DifferenceObject, the following code is required:
Compare-Object -ReferenceObject $a -DifferenceObject $b |
Where-Object { $_.SideIndicator -eq '<=' } |
ForEach-Object { Write-Output $_.InputObject }
The where-object clause ensures only objects that are present in ReferenceObject are passed down the pipeline.
The foreach-object clause forces the output back to a simple array (ref: Converting Custom Object arrays to String arrays in Powershell - thanks Keith)
$c = $a | Where-Object { $b -notcontains $_ }
This should do the job.
Some Explanation
Where-Object's code block tests each element of the array that's piped into it. The block is supposed to return a boolean value, and if it's true, then the result of the call will contain the item in question.
So for the conditional, we use the -notcontains operator with your second array. $_ refers to the individual item from $a.
This doesn't require an additional or nested loop.