[英]Delete first two columns in CSV with PowerShell
I need to delete the first two columns in a CSV. 我需要删除CSV中的前两列。 I do not know the header names, they are not static. 我不知道标题名称,它们不是静态的。 I figured out how to remove the first two rows, but not the first two columns. 我想出了如何删除前两行,而不是前两列。 The sample code I am working with is below 我正在使用的示例代码如下
$csv = Import-Csv 'input.csv'
$headers = $csv[0].PSObject.Properties | select -Expand Name
$step = 4
for ($i = 0; $i -lt $headers.Count; $i += $step) {
$csv | select $headers[$i..($i+$step-1)] |
Export-Csv "output_$($i/$step).csv" -NoType
}
If you wanted to skip the first X columns you just need to adjust your $headers
statement 如果您想跳过前X列,则只需调整$headers
语句
$headers = $csv[0].PSObject.Properties | select -Skip 2 -Expand Name
The above would skip the first 2 columns by not processing the first 2 headers. 上面将不处理前2个标题而跳过前2列。 The rest of the code will group columns of 4 after those 2. 其余代码将在这2之后的4列进行分组。
I think just changing the 0 in the for loop to a 2 should do it: 我认为只需将for循环中的0更改为2即可:
$csv = Import-Csv 'input.csv'
$headers = $csv[0].PSObject.Properties | select -Expand Name
$step = 4
for ($i = 2; $i -lt $headers.Count; $i += $step) {
$csv | select $headers[$i..($i+$step-1)] |
Export-Csv "new_output_$($i/$step).csv" -NoType
}
This seems to work: 这似乎可行:
$csv = Import-Csv 'input.csv'
$include = $csv[0].psobject.properties | select -ExpandProperty Name -Skip 2
$csv | select $include -skip 2 | export-csv output.csv -NoTypeInformation
That should take care of pruning off the first 2 columns and then skipping the first 2 rows. 应当注意删除前两列,然后跳过前两行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.