从CSV文件的字符串列中删除新行字符 [英] Remove New Line Character from CSV file's string column

查看:349
本文介绍了从CSV文件的字符串列中删除新行字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含字符串列的CSV文件,该列跨越多行。

 <$ c $ 

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



我希望我的输出为

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



我想使用PowerShell实现这个输出



>

解决方案

尝试:

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

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

如果您的CSV包含标题,请删除从导入中导入头标'ID','Value','Name',并用实际列名替换 Value
$ b

如果你不想在字段周围加双引号,你可以通过替换 Export-Csv 来删除它们:

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

Out-File 之前添加另一个过滤器以跳过第一行的输出:

  ... | select -Skip 1 | Out-File $ 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.

解决方案

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

这篇关于从CSV文件的字符串列中删除新行字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆