Share via


Changing nth character for each item of a list in powershell

Question

Tuesday, November 1, 2016 3:05 PM

Hi, I have a list like this

ABC123;QC

DEF456;QC

GHI789;QC

and I want to change the 2nd and the 3rd character for * so mi list would become

A**123;QC

D**456;QC

G**789;QC

How can I do that in powershell?

All replies (4)

Tuesday, November 1, 2016 3:24 PM

We do not ****** on request  :)

PS: i would propably use Substring and replace method


Tuesday, November 1, 2016 3:27 PM | 1 vote

$s = 'ABC123;QC'
$s.Remove(1,2).Insert(1,'**')

\(ツ)_/


Tuesday, November 1, 2016 5:36 PM

My list is under D:\script\TEST.txt so I used part of your solution and now it's working thanks!!

$test = "D:\script\TEST.txt"
$sub ="D:\script\TEST2.txt"
Clear-Content $sub

$data = Get-Content -Path $test 
foreach ($line in $data)
{
    $plate=($line.Remove(1,2).Insert(1,'**'))
    Add-content $sub $plate
}


Tuesday, November 1, 2016 5:45 PM

Time to learn PowerShell:

Get-Content D:\script\TEST.txt | %{ $_.Remove(1, 2).Insert(1, '**') } | Out-File D:\script\TEST2.txt

\(ツ)_/