Modify a Powershell script to relpace a character

Member_2_5306354 used Ask the Experts™
I would like to modify the attached script so that when it sees 01-ABCDA, or 01ABCDB, 'and' if sees either 01230c, or 01232 in position 100, that it will change the last character from whatever 3 digits it sees at the end of those lines, to a ,008.

No other functions in the script are needed, so I am sure many of the lines will have to be removed.....

$inFolder = '\\saved\data'
$outSuffix = '_rev'

$referenceCount = 0
$row = 0
Get-ChildItem -Path $inFolder -Filter *.csv | Where-Object {$_.BaseName -notmatch "$($outSuffix)\Z"} | ForEach-Object {
	$outFile = Join-Path -Path $_.DirectoryName -ChildPath "$($_.BaseName)$($outSuffix)$($_.Extension)"
	$saveFile = $false
	Write-Host "Processing '$($_.Name)' ..."
	$content = Get-Content -Path $_.FullName | ForEach-Object {
		$fieldChanged = $false
		$fields = $_.Split(',')
		If ($referenceCount -eq 0) {$referenceCount = $fields.Count}
		If ($fields.Count -ne $referenceCount) {
		   Throw "Field count in line $($row) is incorrect (is: $($fields.Count), expected: $($referenceCount))!"
		$fields = $fields + ''
		$old3 = $fields[3]
		If ($addLastField.Keys -contains $fields[3]) {
			$fields[-1] = $addLastField[$fields[3]]
			$fieldChanged = $true

		If ($fieldChanged) {
			Write-Host "    line $($row): $($old3) [$($fields[26])]) --> $($fields[3]); last: $($fields[-1])"
			$saveFile = $true
		$fields -join ','
	If ($saveFile) {
		$content | Set-Content -Path $outFile
		Write-Host "Changes written to $($outFile)."
	} Else {
		Write-Host "No changes found."

Open in new window

This is the input script in a .csv file.

5,A,01234,01-ABCDA,Y,1251/2,1772345,,20181003,20181107,20181114,,1234567890000,COMP ACCOUNTS PAYABLE,123 ROAD WAY,,TREEWAY,FL,01234-5678,,,,1234564870004,Somewhere Warehouse,200 Treeview Court NW,,Everywhere,GA,12345-1234,,,018-441-5522,41245,ABCORPBC,,USD,N,,,,,,,,,,N,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,00987894561230,01230c,152324,STEEL,,,,123.00,US,0.00,US,0.00,US,10.5000,0.0000,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,101
5,A,01234,01-ABCDB,Y,1251/2,1772345,,20181003,20181107,20181114,,1234567890000,COMP ACCOUNTS PAYABLE,123 ROAR WAY,,TREEWAY,FL,01234-5678,,,,1234564870004,Somewhere Warehouse,200 Treeview Court NW,,Everywhere,GA,12345-1234,,,018-441-5522,41245,ABCORPBC,,USD,N,,,,,,,,,,N,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,00987894561560,01232,152345,WOOD,,,,789.00,US,0.00,US,0.00,US,10.5100,0.0000,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,102
5,A,01234,67891,Y,1253/2,1775478,,20181003,20181107,20181114,,1234567890000,OTHERCOMP ACCOUNTS PAYABLE,123 ROAD WAY,,FOREST,TX,01234-5678,,,,1234564870004,Another Warehouse, 300 Apt View Court NW,Specific Place,TX,12345-5678,,,018-441-5522,41245,DEFORPBC,,USD,N,,,,,,,,,,N,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,00987894564560,00456,152322,CANVAS,,,,123.00,US,0.00,US,0.00,US,10.5200,0.0000,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,103
5,A,01234,55555,Y,1254/2,1771234,,20181003,20181107,20181114,,1234567890000,OTHEROTHERCOMP ACCOUNTS PAYABLE,123 ROAD WAY,,JUNGLE,GA,01234-9999,,,,1234564870004,BAnother Warehouse, 400 Sea View Court NW,AnotherSpecific Place,GA,12345-9999,,,018-441-5522,41245,GHIORPBC,,USD,N,,,,,,,,,,N,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,00987894564560,00456,152322,CANVAS,,,,123.00,US,0.00,US,0.00,US,10.5200,0.0000,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,104
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Citrix Engineer
Unless I'm missing something.. this sounds like it should be pretty straightforward?   Starting from Line 10..
This is based on Get-Content reading things one line at a time..
$OutputContent = [system.collections.arraylist]::New()
Get-Content -Path $_.FullName | ForEach-Object {
     $LineContent = $_.Split(',')
     if (($LineContent[99] -eq '01230c' -or $LineContent[99] -eq '01232') -and ($LineContent -contains '01-ABCDA' -or $LineContent -contains '01ABCDB' ))
          $LineContent[-1] = '008'
     [void]$OutputContent.Add($($LineContent -join ','))
Set-Content -Path $outFile -Value ($OutputContent -join "`r`n")

Open in new window

Now.. this was just the replacement code..  I'm not sure how much of the rest of it you need.. but this should take care of the logic.  If you need some of that other logic, then we need to know what else needs to be preserved & reworked.  


Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial