简体   繁体   English

使用powershell删除.csv文件中的最后一行

[英]Use powershell to remove last row in .csv file

I am currently in need of a solution for one of our automation scripts. 我目前需要一个自动化脚本的解决方案。 As part of the process it grabs the file from a source and it has to remove last row in the .csv and save it again as csv file(not sure why this has to be done, as file already comes as .csv, but without it - it bugs out). 作为进程的一部分,它从源获取文件,并且必须删除.csv中的最后一行并将其再次保存为csv文件(不知道为什么必须这样做,因为文件已经作为.csv,但没有它 - 它错了)。 The problem is that I can't seem to figure out how to remove the last row. 问题是我似乎无法弄清楚如何删除最后一行。 I came up with few solutions but most only work on .txt file or objects. 我想出了一些解决方案,但大多数只能处理.txt文件或对象。 What I have now is this: 我现在拥有的是:

Import-Csv $file | where {$_.'Search Status' -ne "Blank line"} | Export-Csv "\file.csv" -notypeinfo

It does delete the last line that has ' Blank Line' written in it, but it seems to just remove the text and when my other script parses the file it fails to do so properly. 它确实删除了写在其中的“空行”的最后一行,但它似乎只删除了文本,当我的其他脚本解析文件时,它无法正确执行。 So to sum up - I need a script that would take this file, remove last row completely and save it again as .csv as 'export-csv' doesn't seem to do the job. 总而言之 - 我需要一个可以获取此文件的脚本,完全删除最后一行并将其再次保存为.csv,因为'export-csv'似乎不能完成这项工作。

In PSv5+ you can use Select-Object 's -SkipLast parameter: PSv5 +中,您可以使用Select-Object-SkipLast参数:

Get-Content $file | Select-Object -SkipLast 1 | Set-Content out.csv -Encoding UTF8

Note that I'm simply using Get-Content rather than Import-Csv , as there's no good reason to incur the overhead of Import-Csv (construction of a custom object for each input line) and subsequent Export-Csv (serialization of each custom object to a comma-separated list of values in a single string) if the only task required is to drop a line as a whole. 请注意,我只是使用Get-Content而不是Import-Csv ,因为没有充分的理由可以产生Import-Csv的开销(为每个输入行构建自定义对象)以及后续的Export-Csv (每个自定义的序列化)如果所需的唯一任务是作为一个整体删除一行 ,则对象以逗号分隔的单个字符串中的值列表。

Also note that I've included an -Encoding argument to illustrate the point that you may want to control the output encoding explicitly, because the input encoding is never preserved in PowerShell. 另请注意,我已经包含了一个-Encoding参数来说明您可能希望显式控制输出编码,因为输入编码永远不会保留在PowerShell中。
Set-Content will default to "ANSI" encoding on Windows PowerShell, and BOM-less UTF-8 on PowerShell Core. Set-Content在Windows PowerShell上默认为“ANSI”编码,在PowerShell Core上默认为无BOM的UTF-8。


In PSv4- , you can employ the following technique to emulate -SkipLast 1 ; PSv4-中 ,您可以使用以下技术来模拟-SkipLast 1 ; it allows you to do the same in a single pass too, and without needing to load the entire input file into memory: 它允许您在单个传递中执行相同操作,而无需将整个输入文件加载到内存中:

Get-Content $file | ForEach-Object { $prev = $null } {      
  if ($null -ne $prev) { $prev }
  $prev = $_
} | Set-Content out.csv -Encoding UTF8

The above simply delays the output of the pipeline input object by one iteration, which effectively omits the last input object. 以上简单地将管道输入对象的输出延迟一次迭代,这有效地省略了最后一个输入对象。

You can load the file into an object, remove the last line, and then re-write it: 您可以将文件加载到对象中,删除最后一行,然后重新编写它:

$f = @(Get-Content -Path $file)
[array]::Reverse($f)
$f = $f | Select-Object -Skip 1
[array]::Reverse($f)
$f | Out-File -FilePath $file

Note: this is extremely lazy (and performs poorly) on my part because I didn't spend more time thinking about it, but it gets the job done. 注意:这对我来说非常懒惰(并且表现不佳),因为我没有花太多时间考虑它,但它完成了工作。

  • Import the csv 导入csv
  • get the item count 得到项目数
  • Select the first (count - 1) Rows 选择第一行(count-1)行
  • export 出口

$FileIn = '.\file.csv'
$FileOut= '.\file2.csv'
$csv = Import-Csv $FileIn
$csv | Select-Object -First ($csv.count -1) |
    Export-Csv $FileOut  -NoTypeInformation

or do 或者做

$x = @(Get-Content -Path $file)
($x[0..($x.Length - 2)]) | Out-File -FilePath $file

to save from doing the [array]::Reverse() twice 从执行[array]::Reverse()两次保存

根据您的powershell版本,您可以使用Select-Object的skiplast参数

Import-Csv $file | where {$_.'Search Status' -ne "Blank line"} | select -skiplast 1 | Export-Csv "\file.csv" -notypeinfo

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

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