Avatar of E=mc2
E=mc2
Flag for Canada asked on

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

PowershellWindows 10Scripting Languages

Avatar of undefined
Last Comment
Bill Prew

8/22/2022 - Mon
oBdA

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

ASKER CERTIFIED SOLUTION
Bill Prew

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
E=mc2

ASKER
Perfect thanks very much, worked excellent.
Bill Prew

Welcome.


»bp
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck