Forum Discussion

manny213's avatar
manny213
Brass Contributor
Nov 09, 2024
Solved

Remove a comma from a CSV file for a particular column only

Hi everyone I have a CSV file that has some bad data in a particular column called ColumnA.  ColumnA is a column of names.  Some of the names have a comma in it.  For example, "Invesco QQQ Trust, Se...
  • LainRobertson's avatar
    LainRobertson
    Nov 10, 2024

    Hi manny213 ,

     

    You can't provide a wildcard specification such as "*.csv" to the Import-Csv commandlet. That's why you're getting that error.

     

    Given Import-Csv can only accept a single file, you have to wrap it within an outer ForEach-Object statement.

     

    Get-ChildItem -Path "d:\data\temp\forum\*.csv" | ForEach-Object {
        # Name of the temporary working file.
        $tempFile = [regex]::Replace($_.FullName, ".csv$", "-temp.csv");
    
        try
        {
            # Parse the original file and pipe to the temporary file.
            Import-Csv -Path $_.FullName -ErrorAction:Stop | ForEach-Object {
                [PSCustomObject] @{
                    SYMBOL = $_.SYMBOL;
                    INTERVAL = $_.INTERVAL;
                    NAME = $_.NAME.Replace(",", "");
                    DATE = $_.DATE;
                    OPEN = $_.OPEN;
                    HIGH = $_.HIGH;
                    LOW = $_.LOW;
                    CLOSE = $_.CLOSE;
                    VOLUME = $_.VOLUME;
                }
            } | Export-Csv -Path $tempFile -NoTypeInformation -ErrorAction:Stop;
    
            # Remove original file.
            Remove-Item -Path $_.FullName -ErrorAction:Stop;
    
            # Rename temporary file to the original file's name.
            Rename-Item -Path $tempFile -NewName $_.Name -ErrorAction:Stop;
        }
        catch
        {
            Write-Error -Exception $_.Exception;
        }
    }

     

    This basic script will attempt to parse the original file before replacing it, and will throw an error if it's unable to do so.

     

    It will repeat this process for each file within the directory, as illustrated by the outer ForEach-Object block on line 1, which is where you can specify wildcards via the Get-ChildItem commandlet.

     

    This is the sample original file I used.

     

     

    This is the resulting file after parsing.

     

     

    Cheers,
    Lain

Resources