1

I'm on a project where I need to match a string for a certain substring within a text file. I'm using PowerShell. I found this, and this gave me a great start.

Check if a string contains any substring in an array in PowerShell

From this, I've been able to find a single substring in a single string, and have been trying (see below) to extend this to finding several substrings in an array of strings. The array of strings are column names in a DB table; I need to compare these to data in a text file, where each line (in the text file) will an entry in the DB. To futher clarify, here are two lines of code that I'm using:

$columnNames = ("Col_1","Col_2","Col_3","Col_4","Col_5")
$fileData = Get-Content dataRetrieved.txt

The text file (dataRetrieved.txt) contains data shown below, but only a small part of the data is left-justified; I could not figure out how to correctly illustrate how it appears in a text editor.:

Col_1=this is some type of data

Col_2=more stuff that was collected

Col_3=even more data that might continue on another line and probably indented

Col_5=ABCDEFG123

Col_1=Some more stuff

Col_2=yada yada

EDIT: Example of this file:

Col_1=this is some type of data
 Col_2=more stuff that was collected

Col_3=even more lines of data
     that continues on another
     Col_5=ABCDEFGi23
  Col_1=Some more stuff
Col_2=yada yada
...

Note #1: there will be "irregularities" in the text file - no control over how the users input the data.

Note #2: there is no limit to how many lines in the text file

Note #3: there may/may not be values foreach of $columnNames

BIG NOTES:

I'm a standard user on the system, and cannot install/configure any software/packages

The system admins will not install anything

From a programming standpoint I have PowerShell, Java (somewhat limited, e.g. no JDBC, nor will it be installed)

Code that I have tried:

$columnNames = ("Col_1","Col_2","Col_3","Col_4","Col_5")
$fileData = Get-Content dataRetrieved.txt
for ($k=0;$k -lt $columnNames.Count;$k++) {
    if ([bool]($fileData[$k] -match $columnNames[$k])) {
        #code to process the "match"
    }
}

What I'm seeing doesn't make sense to me. Outside of the for loop, statements like $columnNames[0] or $fileData[0] will give me what I believe to be the correct data. Inside the for loop, both $columnNames[$k] and $fileData[$k] give me the entire array.

Appreciate any/all help.

4
  • Can you add some more details on the contents of this dataRetrieved.txt file ? (see my mis-use of the answer component of SO ) Commented Sep 12, 2023 at 15:18
  • "the entire array" What you you mean? $columNames[3] (when $k=3) should return "Col_4" and not an entire array Commented Sep 12, 2023 at 16:10
  • @zett42 - thanks for the error catch!! Commented Sep 12, 2023 at 16:38
  • @Solaris_Guy: You could use edit to correct the typo .... (Thanks!) Commented Sep 12, 2023 at 17:26

1 Answer 1

1

I would use a regex pattern for all desired match criteria with a named capture group.

# the parenthesis was not needed around this list
$columnNames = "Col_1","Col_2","Col_3","Col_4","Col_5"

# create a single multi value pattern. Will look like this (?<Column>Col_1|Col_2|Col_3|Col_4|Col_5)
$pattern = '(?<Column>{0})' -f ($columnNames -join '|')

# just as an example we'll write out the specific column that matched
# followed by the line that matched. 
foreach($line in $filedata){
    if($line -match $pattern){
        Write-Host The column that matched is $matches.column
        Write-Host The line that matched is $line
    }
}

Output with your sample data

The column that matched is Col_1
The line that matched is Col_1=this is some type of data
The column that matched is Col_2
The line that matched is  Col_2=more stuff that was collected
The column that matched is Col_3
The line that matched is Col_3=even more lines of data
The column that matched is Col_5
The line that matched is      Col_5=ABCDEFGi23
The column that matched is Col_1
The line that matched is   Col_1=Some more stuff
The column that matched is Col_2
The line that matched is Col_2=yada yada
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.