Modify powershell script to look for specific digits and write in a specific field

I would like to change this script so that when lookin at position position 99,  it looks only at the last 5 digits and then writes those 5 digits in position 100...



Param(
	$filename = 'c:\Temporary\Import1.csv',
	$newFile  = 'c:\Temporary\Import2.csv'
)
$content = Get-Content $filename

$outRecords = @()

foreach( $record in $content )
{
	$array = $record.Split( ',' )
	## entries in $array are zero-based
	if ($array.Length -lt 101) {
		$outRecords += $record
	} else {
	     if ($array[3] -eq '65-ABCDEF' ) {
		if ($array[99] -eq "") {
		      if ($array[98].Length -gt 5) {
			$array[99] = $array[98].substring($array[98].length-6,5)
		      }
		}
	     }
	     $outRecords += $array -join ","
	}
}
$outRecords | Out-File $newFile -Encoding ASCII

Open in new window

100questionsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Like this?
Param(
	$filename = 'c:\Temporary\Import1.csv',
	$newFile  = 'c:\Temporary\Import2.csv'
)
$content = Get-Content $filename

$outRecords = @()

foreach ($record in $content) {
	$array = $record.Split(',')
	## entries in $array are zero-based
	if ($array.Length -lt 101) {
		$outRecords += $record
	} else {
		if ($array[3] -eq '65-ABCDEF' ) {
			if (($array[99] -eq "") -and ($array[98].Length -gt 5)) {
				$array[99] = $array[98].Substring($array[98].length-6, 5)
			}
			if ($array[99] -match '\d{5}$')) {
				$array[100] = $Matches[0]
			}
		}
		$outRecords += $array -join ","
	}
}
$outRecords | Out-File $newFile -Encoding ASCII

Open in new window

Bill PrewIT / Software Engineering ConsultantCommented:
Try this, it sounds like you just want the right 5 positions of column 100 rather than the first 5 positions of the right 6 characters,

Param(
	$filename = 'c:\Temporary\Import1.csv',
	$newFile  = 'c:\Temporary\Import2.csv'
)
$content = Get-Content $filename

$outRecords = @()

foreach( $record in $content )
{
	$array = $record.Split( ',' )
	## entries in $array are zero-based
	if ($array.Length -lt 101) {
		$outRecords += $record
	} else {
	     if ($array[3] -eq '65-ABCDEF' ) {
		if ($array[99] -eq "") {
		      if ($array[98].Length -gt 5) {
			$array[99] = $array[98].substring($array[98].length-5,5)
		      }
		}
	     }
	     $outRecords += $array -join ","
	}
}
$outRecords | Out-File $newFile -Encoding ASCII

Open in new window


»bp

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
100questionsAuthor Commented:
Perfect thanks very much, worked excellent.
Bill PrewIT / Software Engineering ConsultantCommented:
Welcome.


»bp
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.