0

Im trying to find a way using Select-String and search for exact match in a file, and get all the lines that contains this exact match in exact colum. Example Snippet from data file:

08/25/2021 11:56:03 muzi.puzi 123 Bla
08/25/2021 19:56:03 bla.bla 1234 Bla
08/25/2021 19:56:03 abc.def 54 bla123
Function Get_User_Dates_From_DBFile ([int] $Num, [string] $Db_File){
    #Rereive all the dates in DataFile BiometricNum has signed on or off
    $Dates = gc $Db_File | Select-String -Pattern "^$BioMetric_Num" 
    Return ($Date | Sort | Get-Unique) 
}

Get_User_Dates_From_DBFile 123 $Db_file

This will give me all Three lines, I want to make sure that it doesnt catch anything more than that (Example 1234) and also make sure it doesnt catch 123 at the end of the last line, as the nubers is refering only to the 4th colounm

Any Advice would be great, Thanks

2
  • 1
    Define: "exact match". I guess you mean a whole word that bound by word bounderies: "\b$num\b" Commented Aug 24, 2021 at 7:27
  • This is exactly what i mean, Thank you!\ Commented Aug 24, 2021 at 7:48

1 Answer 1

1

I'm not sure why you are using the roof sign before the variable...
But this pattern will eleminate wrong 123

'08/25/2021 11:56:03 muzi.puzi 123 Bla
08/25/2021 19:56:03 bla.bla 1234 Bla
08/25/2021 19:56:03 abc.def 54 bla123' > file.txt

$BioMetric_Num = '123'

Get-Content .\file.txt | Select-String -Pattern " $BioMetric_Num "

So in your case, your current function should be able to work correctly using

Get_User_Dates_From_DBFile ' 123 ' $Db_file

Sign up to request clarification or add additional context in comments.

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.