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.
dataRetrieved.txtfile ? (see my mis-use of the answer component of SO )$columNames[3](when $k=3) should return "Col_4" and not an entire array