jdubs
jdubs

Reputation: 41

Replace variable in CSV using Powershell

I have a messy CSV file that gets generated each week. This is what I start with:

Company,Division,Department,Location,Job Title,Username
1-Acme INC,9000-CSD,6003-CSD IT,2-Downtown,IT Technician,user1
1-Acme INC,9000-CSD,6002-CSD HR,2-Downtown,HR Generalist,user2

What I really want is this:

Company,Division,Department,Location,Job Title,Username
Acme INC,CSD,CSD IT,Downtown,IT Technician,user1
Acme INC,CSD,CSD HR,Downtown,HR Generalist,user2

Basically I just want to strip the "[numeric]-" portion of that field. I have searched high and low and found different variations of replacing a CSV string with powershell, but haven't found the right syntax to replace variable text in a CSV using powershell.

This is what I have so far. When I check the output, nothing has changed.

$csv = Import-Csv .\test.csv
$csv | %{ $_ -replace "^[0-9]+-"}
$csv| Export-Csv .\test.csv -NoTypeInformation -Force

Any help would be greatly appreciated!

Upvotes: 2

Views: 400

Answers (1)

Tony Hinkle
Tony Hinkle

Reputation: 4742

You need to use:

$csv = get-content .\test.csv
$csv = $csv -replace "[0-9]+-", ""
$csv | out-file .\test.csv

or line 2 could be just:

$csv = $csv -replace "[0-9]+-"

Upvotes: 2

Related Questions