2017-02-18 101 views
0

此处的Powershell总计noob。请帮忙!Powershell添加列,删除行和填充CSV文件中的文本

我有一个CSV文件看起来像这样:

Office,First Name,Last Name,email 
Pancacke Group,Lidia,Gheno,[email protected] 
Miller Tracy Carmel Valley,Paulina,Mastros,[email protected] 

我需要有一个PowerShell的方式,使它看起来像这样:

,,,Pancacke Group,Test,Lidia,Gheno,[email protected] 
,,,Miller Tracy Carmel Valley,Test,Paulina,Mastros,[email protected] 

总结需求:

  1. 在第一列前加三列,
  2. 加上anoth办公室和名之间ER列
  3. 填写办公室和名之间的列字Test
  4. 删除第一行与包头

预先感谢您的帮助!

+0

'(GC in.csv)-replace '^(*?)', ',,, $ 1,测试,' |选择-skip 1 | sc out.csv' – TessellatingHeckler

回答

0

解决方案1 ​​

import-csv "C:\temp\test\test.csv" | 
    select col1, col2, col3, Office, @{N="Col4";E={"Test"}}, 'First Name', 'Last Name', email | 
     ConvertTo-Csv -NoTypeInformation | 
      select -skip 1 | 
       out-file "C:\temp\test\result.csv" 
+0

这工作完美! –

0
Import-Csv file.csv | Select-Object "col1", "col2", "col3" , Office, "col4", "First Name", "Last Name", email | Export-Csv file.csv -NoTypeInformation 

上面的一个班轮在开始时预先设置三列,在办公室和名字之间预设三个列。下一步删除第一行:

Get-Content file.csv | select -Skip 1 | out-file file.csv 

两个步骤,一个线:

Import-Csv file.csv | Select-Object "col1", "col2", "col3" , Office, "col4", "First Name", "Last Name", email | ConvertTo-Csv -NoTypeInformation | Select-Object -Skip 1 | out-file file.csv 

希望这是你想要的东西。

0

解决方案2

get-content "C:\temp\test\test.csv" | 
    %{$Cols=$_ -split "," ; ",,,{0},Test,{1},{2},{3}" -f $Cols[0],$Cols[1],$Cols[2],$Cols[3]} | 
     out-file "C:\temp\test\result.txt"