简体   繁体   English

修剪使用PowerShell CSV单元串

[英]Trim csv cell string with Powershell

I am having trouble using the .Trim() function on a column within a csv file. 我在csv文件中的列上使用.Trim()函数时遇到问题。 This csv file only contains one column in the whole table so it should be pretty straight forward (he says). 这个csv文件在整个表格中仅包含一列,因此应该非常简单(他说)。

The data within my csv, Exhibit A ( NOTE: This is all in one field/column, not separate columns ): 我的csv中的数据,图表A( 注:全部在一个字段/列中,而不是单独的列中 ):

Name
C:\Users\kgroome\Documents\NOC\Documentation\Chrome, .pdf
C:\Users\kgroome\Documents\NOC\Documentation\CLI, .docx
C:\Users\kgroome\Documents\NOC\Documentation\DNS, .pdf
C:\Users\kgroome\Documents\NOC\Documentation\Encryption, .pdf
C:\Users\kgroome\Documents\NOC\Documentation\Excel, .xlsx

Ideally I need to trim everything from right to left after the final 5 characters within the string OR split after the , as this would be much more ideal 理想情况下,我需要修剪一切从右侧后的字符串或组内的最后5个字符后离开,因为这将是非常理想的多

What I currently have in regards to syntax for splitting after the , is the following: 我目前在关于语法后分裂,如下:

$data = Import-Csv "C:\Support\Test05.csv"

foreach($line in $data){

    $line.split(',')[5].Trim(); |

    Export-Csv -Path "C:\Support\Test06.csv"

 }

Albeit this isn't my best, I have had previous syntax for both methods however I have scrapped it because it was near useless. 虽然这不是我最好的,我有两种方法,但是我已经报废了,因为这是近没用以前的语法。

Your help would be extremely appreciated so I can know why I am going wrong! 您的帮助将是非常赞赏这样我就可以知道为什么,我错了!

The fact that you are using a CSV file is a bit of a red herring and I think you want to use replace insread of split: 您正在使用CSV文件的事实有点让人讨厌,我想您想使用split的replace insread:

Get-Content "C:\Support\Test05.csv" | % {
    $_ -replace '\s*,\s*', ','
} | Set-Content "C:\Support\Test06.csv"

I'm only suggesting this to help you understand where your code was not functioning properly. 我只是建议这对帮助您了解您的代码是不正常。 I am still not completely sure what your output is supposed to look like and there are better ways to do this (like with regex) 我还没有完全确定你的输出应该看起来像和有更好的方法来做到这一点(如使用正则表达式)

$file = "c:\temp\text.csv"
$newfile = "c:\temp\text1.csv"

Get-Content $File | ForEach-Object{
    If($_ -match ","){
        $_.Split(",").Trim()[1]
    } Else {
        $_
    }
} | Set-Content $newfile

Output 输出量

Name
.pdf
.docx
.pdf
.pdf
.xlsx

Split creates an array from a string based on the delimiter you specify. 斯普利特创建基于您指定的分隔符的字符串数组。 Splitting on a comma with your sample data will only create an array with two elements. 与您的样本数据逗号分割只会造成两种元素的数组。 Requesting the sixth element with 5 will simply generate a null. 与请求所述第六元件5将简单地产生一个空值。

As for a regex solution this would suffice and generate the same output in the original file. 作为一个正则表达式的解决方案,这将是足够的,并生成原始文件相同的输出。

(Get-Content $File) -replace ".*,\s?" | Set-Content $file

The regex here would grab all characters up until the comma as well as the following space if present. 这里的正则表达式将抓住所有的字符,直到逗号以及如果存在下面的空间。 Since the match requires the comma to be there the first row with "Name" is skipped as it does not match. 由于比赛需要逗号在那里的第一行有“姓名”跳过,因为它不匹配。

Maybe this: 也许这样:

gc "C:\Support\Test05.csv" | 
        % { $_.split(',')[-1].trim()} | 
                 set-content "C:\Support\Test06.csv"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM