powershell保留csv中的特定行

时间:2016-04-28 16:36:01

标签: csv powershell

我有一个csv文件,如下所示。我需要丢失页眉和页脚并保留数据。我可以用行数来做,但页眉和页脚将永远不会相同。文件长度会不断变化。非常感谢任何帮助。

示例CSV:

,Table 1-4,,,,,,,,,,,
,Some BS,,,,,,,,,,,
,More crap that doesn't matter,,,,,,,,,,,
,"Blah Blah Inc. ",,,,,,,,,,,
,"Year 2014, Quarter 1",,,,,,,,,,,
,,,,,,,,,,,,
,ID,First Name,Last Name,Sex1,Birth date,Flag,Some Number ,Individual Thing ,,,,
,456435634A,ALBERT ,SMITH ,1 ,07/24/1964 ,0 ,742109842 ,18018,,,,
,745675674A,ALBERT ,SMITH ,1 ,07/24/1964 ,0 ,742109842 ,17808,,,,
,234523455A,CARLTON ,JONES ,1 ,10/04/1953 ,0 ,742109842 ,15088,,,,
,567567567D,ANITA ,COX ,2 ,10/09/1967 ,0 ,742109842 ,19321,,,,
,6578678885A,SUSAN ,WOOD ,2 ,07/23/1934 ,0 ,742109842 ,12758,,,,
,,,,,,,,,,,,
,,,,,,,,,,,,
,,,,,,,,,,,,
,Notes:,,,,,,,,,,,
,"Notes the I dont care about.",,,,,,,,,,,
,See note regarding Definition of PCS in the Parameters Worksheet.,,,,,,,,,,,
,"Some more notes.",,,,,,,,,,,
,"Yet even more.",,,,,,,,,,,
,Wow even more and no quotes this time.,,,,,,,,,,,
,,,,,,,,,,,,
,,,,,,,,,,,,
,"Disclaimer blah blah blah.",,,,,,,,,,,

1 个答案:

答案 0 :(得分:0)

这样的事情可以起作用

@'
,Table 1-4,,,,,,,,,,,
,Some BS,,,,,,,,,,,
,More crap that doesn't matter,,,,,,,,,,,
,"Blah Blah Inc. ",,,,,,,,,,,
,"Year 2014, Quarter 1",,,,,,,,,,,
,,,,,,,,,,,,
,ID,First Name,Last Name,Sex1,Birth date,Flag,Some Number ,Individual Thing ,,,,
,456435634A,ALBERT ,SMITH ,1 ,07/24/1964 ,0 ,742109842 ,18018,,,,
,745675674A,ALBERT ,SMITH ,1 ,07/24/1964 ,0 ,742109842 ,17808,,,,
,234523455A,CARLTON ,JONES ,1 ,10/04/1953 ,0 ,742109842 ,15088,,,,
,567567567D,ANITA ,COX ,2 ,10/09/1967 ,0 ,742109842 ,19321,,,,
,6578678885A,SUSAN ,WOOD ,2 ,07/23/1934 ,0 ,742109842 ,12758,,,,
,,,,,,,,,,,,
,,,,,,,,,,,,
,,,,,,,,,,,,
,Notes:,,,,,,,,,,,
,"Notes the I dont care about.",,,,,,,,,,,
,See note regarding Definition of PCS in the Parameters Worksheet.,,,,,,,,,,,
,"Some more notes.",,,,,,,,,,,
,"Yet even more.",,,,,,,,,,,
,Wow even more and no quotes this time.,,,,,,,,,,,
,,,,,,,,,,,,
,,,,,,,,,,,,
,"Disclaimer blah blah blah.",,,,,,,,,,,
'@ | ConvertFrom-Csv -Header h1, ID, FirstName, LastName, Sex1, BirthDate, Flag, SomeNumber, IndividualThing, h10, h11, h12 |
    Where-Object id -Match '^\d+\w$' |
    Select-Object ID, FirstName, LastName, Sex1, BirthDate, Flag, SomeNumber, IndividualThing |
    Export-Csv c:\temp\justthedata.csv -NoTypeInformation

start C:\temp\justthedata.csv

如果您正在使用某个文件,请将此内容和所有内容替换为

... | ConvertFrom-Csv

用这个

import-csv 'C:\temp\file.csv'