Solved

Use Powershell to clean up last names in a csv file

Posted on 2011-03-23
8
971 Views
Last Modified: 2012-06-27
Looking for a way to clean up the "last name" column in a CSV file. Some last names are fine "Johnson", but others are not; "Johnson P." or "Hicks MD" or "Renegar DDS PA" or "Ashcraft,". The file has several columns that I'm looking to maintain untouched, and output them with the "cleaned" version of the column last name values. So the output should be something like this:

BEFORE:
Number1  Number2         First Name    Last Name
100552      B7BA7153A      Darwin          Hayes
105212      15A1A75CA      Craig          Hicks DDS PC
105216      EA1357313      Charles          Chilcoat PC
105220      3A1C571EA      Gary                  Renegar Dds Pa
105221      99E59A31C      Michael          Ashcraft,

AFTER
Number1  Number2         First Name    Last Name
100552      B7BA7153A      Darwin          Hayes
105212      15A1A75CA      Craig          Hicks
105216      EA1357313      Charles          Chilcoat
105220      3A1C571EA      Gary                  Renegar
105221      99E59A31C      Michael          Ashcraft
0
Comment
Question by:bndit
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 16

Accepted Solution

by:
Dale Harris earned 250 total points
Comment Utility
If you're using a CSV, this should work:

$File = import-csv "Book1.csv"
$File2 = @()
foreach ($line in $File){
$LastName = $line.lastname.split(" ")[0]
$FirstName = $line.firstname
$Number1 = $line.number1
$Number2 = $line.number2
$File2 += "$Number1 $Number2 $Firstname $Lastname"
}

$File2

#This will give you what you want in File2.  You can export it to text or CSV.  But play around with it to see what you have.

HTH,

Dale Harris
0
 
LVL 10

Assisted Solution

by:wls3
wls3 earned 250 total points
Comment Utility
This will automatically handle encoding, input file, output file and delimeters (in case you are not using commas).  There is more that could be added, but, this is a good starting point.

Function CleanCSVFile($filename, $delimeter, $output)
{
    $csv = Import-Csv -Path $filename -Delimiter $delimeter
    $b
    foreach($line in $csv)
    {
        $b += $line.Number1 + "," + $line.Number2 + "," + $line."First Name" + "," + $line."Last Name".Split(" ")[0] + "`n"
    }
    Out-File -FilePath $output -Encoding ASCII -InputObject $b
}

Open in new window

It can be used like this:
CleanCSVFile "C:\Users\w\Documents\test.csv" "," "C:\Users\w\Documents\testout.csv"

Open in new window

0
 
LVL 16

Expert Comment

by:Dale Harris
Comment Utility
wls3,

I like how you did the $Line."First Name".  I didn't know that would work.  Thanks for the tip.

DH
0
 
LVL 2

Author Comment

by:bndit
Comment Utility
I'm calling the function as such...but it's failing

CleanCSVFile ('C:\scripts\output\shortHRList.csv',',','C:\scripts\Output\output1.csv')

I'm probably calling it wrong
error.png
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 2

Author Comment

by:bndit
Comment Utility
Ok, false alarm...I wasn't familiar with the way parameters are passed to functions in Powershell..and didn't know that parentheses actually evaluate the values to an expression....I changed my call to this:

CleanCSVFile "C:\scripts\output\shortHRList.csv" "," "C:\scripts\Output\output1.csv"

and it's working...thanks both for your feedback!
0
 
LVL 13

Expert Comment

by:soostibi
Comment Utility
You have to adjust at the import-csv part the path and the delimiter (now it is set to TAB character).
Import-Csv C:\yorfile.csv -Delimiter "`t" | Select-Object number1, number2, "first name", @{n="Last Name"; 
    e = {$_."Last Name" -replace "^(\w+).*",'$1'}}

Open in new window

0
 
LVL 13

Expert Comment

by:soostibi
Comment Utility

Import-Csv C:\yorfile.csv -Delimiter "`t" | Select-Object number1, number2, "first name", @{n="Last Name";   
    e = {$_."Last Name" -replace "^(\w+).*",'$1'}} | export-csv c:\newfile.csv -notypeinformation

Open in new window

0
 
LVL 2

Author Closing Comment

by:bndit
Comment Utility
Good answers.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article shows how a content item can be identified directly or through translation of a navigation type. It then shows how this information can be used to create a menu for further navigation.
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now