简体   繁体   English

将值拆分为两列 powershell csv 文件

[英]Splitting values into two columns powershell csv file

I have a PowerShell script that creates a csv file and neatly separates all user input.我有一个 PowerShell 脚本,它创建一个 csv 文件并巧妙地分隔所有用户输入。 I need the remaining output to be split across the two headers and I'm struggling to find out how.我需要将剩余的 output 拆分到两个标题中,我正在努力找出方法。 Tried lots of different code but had no luck.尝试了很多不同的代码,但没有运气。

$Devices = read-host -Prompt "Enter Full Device Name" | out-file 'C:\Users\Public\Desktop\Devices.csv' -Force
$Find = ", "
$Replace = "`n"
$Arrange = (Get-Content 'C:\Users\Public\Desktop\Devices.csv') -replace "$Find","$Replace" | Set-Content 'C:\Users\Public\Desktop\Devices.csv' -Force
$CSV = import-csv 'C:\Users\Public\Desktop\Devices.csv' -Header "Firstname","Lastname" 
$CSV | Export-Csv -NoTypeInformation 'C:\Users\Public\Desktop\Devices.csv' 
$import = Import-Csv 'C:\Users\Public\Desktop\Devices.csv'

This is the output I currently have in the CSV:这是我目前在 CSV 中的 output:
输出

This is the output I am after:这是我追求的 output:
最终输出

Could almost do with a foreach loop as the first and last names are likely to change as these are inputted using a variable几乎可以使用 foreach 循环,因为名字和姓氏可能会改变,因为这些是使用变量输入的

any help is appreciated.任何帮助表示赞赏。

Export-Csv exports 1 column per distinct property of the input - so to get 2 columns, you need to pipe an object with 2 properties to Export-Csv . Export-Csv为输入的每个不同属性导出 1 列 - 因此要获得 2 列,您需要 pipe 和 object 以及Export-Csv的 2 个属性。

# read device names, split into individual strings
$devices = Read-Host -Prompt "Enter Full Device Name(s)"
$devices = $devices -split ',\s*' |ForEach-Object Trim

# now create one object per device name
$records = $devices |ForEach-Object {
  # start by splitting the string into 2 on the first `-`
  $FirstName,$LastName = $_ -split '-',2

  # now create the object
  [pscustomobject]@{
    FirstName = $FirstName
    LastName = $LastName
  }
}

# ... and finally, export to CSV
$records |Export-Csv 'C:\Users\Public\Desktop\Devices.csv' -NoTypeInformation

If you want to retain the - as part of the FirstName value, change this line:如果您想保留-作为FirstName值的一部分,请更改此行:

$FirstName,$LastName = $_ -split '-',2

to:至:

$FirstName,$LastName = $_ -split '(?<=-)',2

Try this out.试试这个。 Since I don't know what your input file looks like there might be some redundant steps in there.由于我不知道您的输入文件是什么样的,因此其中可能有一些多余的步骤。 Feel free to modify the code to remove any redundant lines if you feel like.如果您愿意,请随意修改代码以删除任何多余的行。

$Devices = read-host -Prompt "Enter Full Device Name" | out-file 'C:\Users\Public\Desktop\Devices.csv' -Force
$Find = ", "
$Replace = "`n"
$Arrange = (Get-Content 'C:\Users\Public\Desktop\Devices.csv') -replace "$Find","$Replace" | Set-Content 'C:\Users\Public\Desktop\Devices.csv' -Force
$CSV = import-csv 'C:\Users\Public\Desktop\Devices.csv' -Header "Firstname","Lastname" 
$X = $CSV | select -Skip 1 
$y = $x -replace '-','-,' # this adds a comma so that the values are in different columns
$y | Export-Csv -NoTypeInformation 'C:\Users\Public\Desktop\Devices.csv'
$import = Import-Csv 'C:\Users\Public\Desktop\Devices.csv' -Header  "Firstname","Lastname"

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

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