[英]PowerShell - importing and splitting just one column in a csv file
I have a csv that contains just two columns (called 'newname' and 'name'):我有一个 csv 只包含两列(称为“新名称”和“名称”):
newname, name新名字,名字
newfile1, file1.pdf新文件1,文件1.pdf
newfile2, file2.txt新文件2,文件2.txt
newfile3, file3.tif新文件 3,文件 3.tif
What I would like to do is to import the csv and split column 2 (name) into two columns, like this:我想做的是导入 csv 并将第 2 列(名称)拆分为两列,如下所示:
newname, name新名字,名字
newfile1, file1, pdf新文件1,文件1,pdf
newfile2, file2, txt新文件2,文件2,txt
newfile3, file3, tif新文件 3,文件 3,tif
I believe that -Delimiter "."我相信 - 分隔符“。” is the way to do this, but I cannot get it to work with the csv import option.
是这样做的方法,但我无法让它与 csv 导入选项一起使用。
This is one approach:这是一种方法:
Import-Csv -Path 'D:\Test\original.csv' |
Select-Object newname,
@{Name = 'name'; Expression = {[IO.Path]::GetFileNameWithoutExtension($_.name)}},
@{Name = 'extension'; Expression = {[IO.Path]::GetExtension($_.name).TrimStart(".")}} |
Export-Csv -Path 'D:\Test\updated.csv' -NoTypeInformation
Result:结果:
newname name extension
------- ---- ---------
newfile1 file1 pdf
newfile2 file2 txt
newfile3 file3 tif
$csv = @"
newfile1, file1.pdf
newfile2, file2.txt
newfile3, file3.tif
"@ | ConvertFrom-Csv -Header "newFileName", "fileName"
$csv | % {
.{[PSCustomObject]@{Name = $Args[0][0]; fileName = $Args[0][1]; ext = $Args[0][2]}} (,$_.newFileName + $_.fileName -split "\.(?!.*\.)")
} | Export-Csv d:\tmp\out.txt
just for Biodiversity只为生物多样性
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.