0

I have many text files like these and below is a sample from one such file. This is part of a file File_Content12.

name:duplicate1-content:philips -sequence:primary...........
name:guard1-content:sony -sequence:primary...........
name:Linux-content:sony -sequence:third...........
name:Windows-content:IPS -sequence:secondary...........
name:Notebook-content:Mark -sequence:fourth...........
name:duplicate1-content:Tony -sequence:primary...........

I'm writing a powershell code to get the name duplicate1 when the content is sony.

I've written the below code and it gets the information as which content is present in which file, line number along with the line.

$contents = 'sony','Philips'
ForEach ($ct in $contents)
{
    Get-Childitem -Path "D:\Input\Files\" | Select-String -Pattern "$ct" | Select Filename,LineNumber,Line,@{n='Content';e={$ct}}
}

I'm stuck on this part to get the name duplicate1 when the content is sony. Do I need to use the IndexOf and Substring to get the value or is there any other way to get this name.

Vignesh
  • 27
  • 1
  • 7

1 Answers1

0

Select-String uses Regular Expression so duplicate.*sony should do what you need

$contents = 'duplicate.*sony','Philips'
ForEach ($ct in $contents)
{
    Get-Childitem -Path "D:\Input\Files\" | Select-String -Pattern "$ct" | Select Filename,LineNumber,Line,@{n='Content';e={$ct}}
}

Run Get-Help about_Regular_Expressions for more details

jfrmilner
  • 1,458
  • 10
  • 11