I have 1 folder filled with .txt files.
I need to find all lines that look like this: NM1PR2AARP MEDICARE COMPLETE*****PI87726~
And replace the LAST field with the value from a database lookup from field 4 like this:
NM1PR2AARP MEDICARE COMPLETE*****PIdatabase value tied to field 4~
I have no idea how to this the smart way so this is what I am doing:
Hmmm … your text pattern is quite confusing - at least for me. What do mean with “fields”? If your source data have a structure you might “re-create” this structure in your script and use it for your good. If your source data do not contain any sensitive data you could post a chunk of it here (formated as code as well please) and we might find a way to help you. What are the criterias for the text pattern to match to one of your 4 database fields?
"Apple|DescriptionToReplace|Boy|FieldToUseInSQLquery" |
Import-CSV -Headers "Fruit","Desc","Gender","SQL" -Delim "|" |
ForEach {
# Perform SQL query
# put new value into $whatever
$_.Desc = $whatever # this changes that column
} |
Export-Csv output.txt -Delim "|" -NoHead
If you let PowerShell take you from text-parsing into structured objects, which is what the -CSV commands already know how to do, life is a lot easier. I’ve applied headers so I can easily refer to the “columns” (properties) by name. Stop thinking “how do I replace a value in a string” and start thinking “how do I turn this string into objects so I don’t have to muck around with strings.”
Oops, that should have been ConvertFrom-CSV, I think, not Import-Csv. Sorry, working from my phone. Anyway, aside from syntax flubs, that’s the pattern to follow.
now if you have multiple finds and multiple replaces, this can be run several times (loop it) I don’t know how processer friendly this would be… I’m not a master at read/write of files, so I’m not sure how it would go line by line (foreach)