Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove New Line Character from CSV file's string column

Tags:

powershell

csv

I have a CSV File with a string column were that column spans to multiple lines. I want to aggregate those multiple lines into one line.

For example

1, "asdsdsdsds", "John"
2, "dfdhifdkinf
dfjdfgkdnjgknkdjgndkng
dkfdkjfnjdnf", "Roy"
3, "dfjfdkgjfgn", "Rahul"

I want my output to be

1, "asdsdsdsds", "John"
2, "dfdhifdkinf dfjdfgkdnjgknkdjgndkng dkfdkjfnjdnf", "Roy"
3, "dfjfdkgjfgn", "Rahul"

I want to achieve this output using PowerShell

Thanks.

like image 570
Varun Gupta Avatar asked Sep 04 '13 17:09

Varun Gupta


People also ask

How do you handle a line break in csv?

To embed a newline in an Excel cell, press Alt+Enter. Then save the file as a . csv. You'll see that the double-quotes start on one line and each new line in the file is considered an embedded newline in the cell.

Does a CSV file have newline characters?

New Line Characters (Reference: https://en.wikipedia.org/wiki/Newline.) If you upload a CSV file that contains new line characters in the fields other than Text Area Field, it will be saved to the system but only Text Area Field preserves the new line if you edit the Asset.

How do I replace a string in a CSV file?

The join() method takes all lines of a CSV file in an iterable and joins them into one string. Then, we can use replace() method on the entire string and can perform single/multiple replacements. In the entire string, the given text is searched and replaced with the specified text.


2 Answers

Building on Ansgar's answer, here's how to do it when:

  • You don't know the column names
  • Your CSV file may contain CR or LF independently

    (Import-Csv $csvInput) | % {
        $line = $_
        foreach ($prop in $line.PSObject.Properties) {
            $line.($prop.Name) = ($prop.Value -replace '[\r\n]',' ')
        }
        $line
    } | Export-Csv $csvOutput -NoTypeInformation
    
like image 120
Joe Zamora Avatar answered Nov 08 '22 04:11

Joe Zamora


Try this:

$csv = 'C:\path\to\your.csv'

(Import-Csv $csv -Header 'ID','Value','Name') | % {
  $_.Value = $_.Value -replace "`r`n",' '
  $_
} | Export-Csv $csv -NoTypeInformation

If your CSV contains headers, remove -Header 'ID','Value','Name' from the import and replace Value with the actual column name.

If you don't want double quotes around the fields, you can remove them by replacing Export-Csv with something like this:

... | ConvertTo-Csv -NoTypeInformation | % { $_ -replace '"' } | Out-File $csv

To remove the header from the output you add another filter before Out-File to skip the first line:

... | select -Skip 1 | Out-File $csv
like image 3
Ansgar Wiechers Avatar answered Nov 08 '22 04:11

Ansgar Wiechers