如何使用powershell将文本文件作为列添加到现有的csv文件中?



我发现了很多这样的问题,解释了如何在整个csv中添加具有相同值的列。

我所拥有的有点不同。我有randlist。txt,它看起来像这样:

24830
4557
30795
19711
15481
42181
17331
32468
39411
30067
5124
35030
17776

有超过40,000个值。与这个allpeeps.csv文件中的行数相同,如下所示

institutiongroup,personid,iscomplete,recordcreateddate,recordupdateddate,lastknowninstitution,personinstitutions,eventprogrammeapplications,customdata,hereadyyear,trackedbyhesa,foundbyhesa,sourceid,nssec,disabilityinfo,yeargroup,yeargroupoffset,expectedheentryyear,lastknownphase,autocalculateheentryyear,cohortyear,recordstateenum,photopermission,howmanyindicsusepolar4,polar4q1,polar4q12,polar4rate,polar4entrants,polar4pop,howmanyindicsusepolar3,polar3ahe,polar4ahe,tundraquintile,imddecile,estdecile,idacidecile,nssec13,nssec47,nssec48,nssecnc
"@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open}",1,TRUE,21/03/2019,21/03/2019,,"     {        institution: {          institutionId: 9921,          displaySummary: Eggbuckland Community College 140104 (#009921),          name: Eggbuckland Community College,          laestabNo: 8794185,          ukprn: 10043058,          urn: 140104,          ucasCode: 11246,          educationPhase: 2,          educationPhaseName: Secondary,          town: Plymouth,          recordState: Open        },        educationPhase: 2,        priority: 0,        recordCreatedDate: 2019-03-21T12:39:06      }  ",System.Object[],{},2023,FALSE,FALSE,,0,,Year Group 12,0,,,TRUE,2023,0,,0 out of 4,N,N,32.7,140,425,0 out of 4,2,3,4,8,9,8,0.61,0.38,0.39,0.06
"@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open}",2,TRUE,21/03/2019,21/03/2019,,"     {        institution: {          institutionId: 9921,          displaySummary: Eggbuckland Community College 140104 (#009921),          name: Eggbuckland Community College,          laestabNo: 8794185,          ukprn: 10043058,          urn: 140104,          ucasCode: 11246,          educationPhase: 2,          educationPhaseName: Secondary,          town: Plymouth,          recordState: Open        },        educationPhase: 2,        priority: 0,        recordCreatedDate: 2019-03-21T12:48:12      }  ",System.Object[],{},2023,FALSE,FALSE,,0,,Year Group 12,0,,,TRUE,2023,0,,4 out of 4,Y,Y,19.6,105,545,4 out of 4,2,1,2,3,2,3,0.36,0.6,0.64,0.06
"@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open}",3,TRUE,21/03/2019,21/03/2019,,"     {        institution: {          institutionId: 9921,          displaySummary: Eggbuckland Community College 140104 (#009921),          name: Eggbuckland Community College,          laestabNo: 8794185,          ukprn: 10043058,          urn: 140104,          ucasCode: 11246,          educationPhase: 2,          educationPhaseName: Secondary,          town: Plymouth,          recordState: Open        },        educationPhase: 2,        priority: 0,        recordCreatedDate: 2019-03-21T13:07:23      }  ",System.Object[],{},2023,FALSE,FALSE,,0,,Year Group 12,0,,,TRUE,2023,0,,1 out of 4,N,Y,27.1,160,585,0 out of 4,4,2,3,7,6,8,0.52,0.46,0.48,0.07
"@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open}",4,TRUE,21/03/2019,21/03/2019,,"     {        institution: {          institutionId: 9921,          displaySummary: Eggbuckland Community College 140104 (#009921),          name: Eggbuckland Community College,          laestabNo: 8794185,          ukprn: 10043058,          urn: 140104,          ucasCode: 11246,          educationPhase: 2,          educationPhaseName: Secondary,          town: Plymouth,          recordState: Open        },        educationPhase: 2,        priority: 0,        recordCreatedDate: 2019-03-21T13:07:24      }  ",System.Object[],{},2023,FALSE,FALSE,,0,,Year Group 12,0,,,TRUE,2023,0,,2 out of 4,Y,Y,19.6,105,545,1 out of 4,2,1,3,7,4,8,0.54,0.44,0.46,0.07
"@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open}",5,FALSE,21/03/2019,21/03/2019,,"     {        institution: {          institutionId: 9921,          displaySummary: Eggbuckland Community College 140104 (#009921),          name: Eggbuckland Community College,          laestabNo: 8794185,          ukprn: 10043058,          urn: 140104,          ucasCode: 11246,          educationPhase: 2,          educationPhaseName: Secondary,          town: Plymouth,          recordState: Open        },        educationPhase: 2,        priority: 0,        recordCreatedDate: 2019-03-21T13:07:24      }  ",System.Object[],{},2023,FALSE,FALSE,,0,,Year Group 12,0,,,TRUE,2023,0,,,,,,,,,,,,,,,,,,
"@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open}",6,TRUE,21/03/2019,21/03/2019,,"     {        institution: {          institutionId: 9921,          displaySummary: Eggbuckland Community College 140104 (#009921),          name: Eggbuckland Community College,          laestabNo: 8794185,          ukprn: 10043058,          urn: 140104,          ucasCode: 11246,          educationPhase: 2,          educationPhaseName: Secondary,          town: Plymouth,          recordState: Open        },        educationPhase: 2,        priority: 0,        recordCreatedDate: 2019-03-21T13:07:25      }  ",System.Object[],{},2023,FALSE,FALSE,,0,,Year Group 12,0,,,TRUE,2023,0,,4 out of 4,Y,Y,20.8,145,705,4 out of 4,2,1,1,4,4,3,0.42,0.53,0.58,0.07
"@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open}",7,TRUE,21/03/2019,21/03/2019,,"     {        institution: {          institutionId: 9921,          displaySummary: Eggbuckland Community College 140104 (#009921),          name: Eggbuckland Community College,          laestabNo: 8794185,          ukprn: 10043058,          urn: 140104,          ucasCode: 11246,          educationPhase: 2,          educationPhaseName: Secondary,          town: Plymouth,          recordState: Open        },        educationPhase: 2,        priority: 0,        recordCreatedDate: 2019-03-21T13:07:25      }  ",System.Object[],{},2023,FALSE,FALSE,,0,,Year Group 12,0,,,TRUE,2023,0,,1 out of 4,N,Y,27.1,160,585,0 out of 4,2,2,2,6,5,7,0.49,0.48,0.51,0.06
"@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open}",8,FALSE,21/03/2019,21/03/2019,,"     {        institution: {          institutionId: 35023,          displaySummary: IKB Academy 142127 (#035023),          name: IKB Academy,          laestabNo: 8004004,          ukprn: 10053995,          urn: 142127,          ucasCode: 19579,          educationPhase: 2,          educationPhaseName: Secondary,          town: Bristol,          recordState: Open        },        educationPhase: 2,        priority: 0,        recordCreatedDate: 2019-03-28T15:47:06      }  ",System.Object[],{},Unknown,FALSE,FALSE,,0,,Unknown,0,,,TRUE,,0,,,,,,,,,,,,,,,,,,
"@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open}",9,FALSE,21/03/2019,21/03/2019,,"     {        institution: {          institutionId: 35023,          displaySummary: IKB Academy 142127 (#035023),          name: IKB Academy,          laestabNo: 8004004,          ukprn: 10053995,          urn: 142127,          ucasCode: 19579,          educationPhase: 2,          educationPhaseName: Secondary,          town: Bristol,          recordState: Open        },        educationPhase: 2,        priority: 0,        recordCreatedDate: 2019-03-28T15:47:07      }  ",System.Object[],{},Unknown,FALSE,FALSE,,0,,Unknown,0,,,TRUE,,0,,,,,,,,,,,,,,,,,,
"@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open}",10,FALSE,21/03/2019,21/03/2019,,"     {        institution: {          institutionId: 35023,          displaySummary: IKB Academy 142127 (#035023),          name: IKB Academy,          laestabNo: 8004004,          ukprn: 10053995,          urn: 142127,          ucasCode: 19579,          educationPhase: 2,          educationPhaseName: Secondary,          town: Bristol,          recordState: Open        },        educationPhase: 2,        priority: 0,        recordCreatedDate: 2019-03-28T15:47:07      }  ",System.Object[],{},Unknown,FALSE,FALSE,,0,,Unknown,0,,,TRUE,,0,,,,,,,,,,,,,,,,,,

我想要的输出是

rand,institutiongroup,personid,iscomplete,recordcreateddate,recordupdateddate,lastknowninstitution,personinstitutions,eventprogrammeapplications,customdata,hereadyyear,trackedbyhesa,foundbyhesa,sourceid,nssec,disabilityinfo,yeargroup,yeargroupoffset,expectedheentryyear,lastknownphase,autocalculateheentryyear,cohortyear,recordstateenum,photopermission,howmanyindicsusepolar4,polar4q1,polar4q12,polar4rate,polar4entrants,polar4pop,howmanyindicsusepolar3,polar3ahe,polar4ahe,tundraquintile,imddecile,estdecile,idacidecile,nssec13,nssec47,nssec48,nssecnc
24830,@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open},1,TRUE,21/03/2019,21/03/2019,,"     {        institution: {          institutionId: 9921,          displaySummary: Eggbuckland Community College 140104 (#009921),          name: Eggbuckland Community College,          laestabNo: 8794185,          ukprn: 10043058,          urn: 140104,          ucasCode: 11246,          educationPhase: 2,          educationPhaseName: Secondary,          town: Plymouth,          recordState: Open        },        educationPhase: 2,        priority: 0,        recordCreatedDate: 2019-03-21T12:39:06      }  ",System.Object[],{},2023,FALSE,FALSE,,0,,Year Group 12,0,,,TRUE,2023,0,,0 out of 4,N,N,32.7,140,425,0 out of 4,2,3,4,8,9,8,0.61,0.38,0.39,0.06
4557,@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open},2,TRUE,21/03/2019,21/03/2019,,"     {        institution: {          institutionId: 9921,          displaySummary: Eggbuckland Community College 140104 (#009921),          name: Eggbuckland Community College,          laestabNo: 8794185,          ukprn: 10043058,          urn: 140104,          ucasCode: 11246,          educationPhase: 2,          educationPhaseName: Secondary,          town: Plymouth,          recordState: Open        },        educationPhase: 2,        priority: 0,        recordCreatedDate: 2019-03-21T12:48:12      }  ",System.Object[],{},2023,FALSE,FALSE,,0,,Year Group 12,0,,,TRUE,2023,0,,4 out of 4,Y,Y,19.6,105,545,4 out of 4,2,1,2,3,2,3,0.36,0.6,0.64,0.06
30795,@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open},3,TRUE,21/03/2019,21/03/2019,,"     {        institution: {          institutionId: 9921,          displaySummary: Eggbuckland Community College 140104 (#009921),          name: Eggbuckland Community College,          laestabNo: 8794185,          ukprn: 10043058,          urn: 140104,          ucasCode: 11246,          educationPhase: 2,          educationPhaseName: Secondary,          town: Plymouth,          recordState: Open        },        educationPhase: 2,        priority: 0,        recordCreatedDate: 2019-03-21T13:07:23      }  ",System.Object[],{},2023,FALSE,FALSE,,0,,Year Group 12,0,,,TRUE,2023,0,,1 out of 4,N,Y,27.1,160,585,0 out of 4,4,2,3,7,6,8,0.52,0.46,0.48,0.07
19711,@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open},4,TRUE,21/03/2019,21/03/2019,,"     {        institution: {          institutionId: 9921,          displaySummary: Eggbuckland Community College 140104 (#009921),          name: Eggbuckland Community College,          laestabNo: 8794185,          ukprn: 10043058,          urn: 140104,          ucasCode: 11246,          educationPhase: 2,          educationPhaseName: Secondary,          town: Plymouth,          recordState: Open        },        educationPhase: 2,        priority: 0,        recordCreatedDate: 2019-03-21T13:07:24      }  ",System.Object[],{},2023,FALSE,FALSE,,0,,Year Group 12,0,,,TRUE,2023,0,,2 out of 4,Y,Y,19.6,105,545,1 out of 4,2,1,3,7,4,8,0.54,0.44,0.46,0.07
15481,@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open},5,FALSE,21/03/2019,21/03/2019,,"     {        institution: {          institutionId: 9921,          displaySummary: Eggbuckland Community College 140104 (#009921),          name: Eggbuckland Community College,          laestabNo: 8794185,          ukprn: 10043058,          urn: 140104,          ucasCode: 11246,          educationPhase: 2,          educationPhaseName: Secondary,          town: Plymouth,          recordState: Open        },        educationPhase: 2,        priority: 0,        recordCreatedDate: 2019-03-21T13:07:24      }  ",System.Object[],{},2023,FALSE,FALSE,,0,,Year Group 12,0,,,TRUE,2023,0,,,,,,,,,,,,,,,,,,
42181,@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open},6,TRUE,21/03/2019,21/03/2019,,"     {        institution: {          institutionId: 9921,          displaySummary: Eggbuckland Community College 140104 (#009921),          name: Eggbuckland Community College,          laestabNo: 8794185,          ukprn: 10043058,          urn: 140104,          ucasCode: 11246,          educationPhase: 2,          educationPhaseName: Secondary,          town: Plymouth,          recordState: Open        },        educationPhase: 2,        priority: 0,        recordCreatedDate: 2019-03-21T13:07:25      }  ",System.Object[],{},2023,FALSE,FALSE,,0,,Year Group 12,0,,,TRUE,2023,0,,4 out of 4,Y,Y,20.8,145,705,4 out of 4,2,1,1,4,4,3,0.42,0.53,0.58,0.07
17331,@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open},7,TRUE,21/03/2019,21/03/2019,,"     {        institution: {          institutionId: 9921,          displaySummary: Eggbuckland Community College 140104 (#009921),          name: Eggbuckland Community College,          laestabNo: 8794185,          ukprn: 10043058,          urn: 140104,          ucasCode: 11246,          educationPhase: 2,          educationPhaseName: Secondary,          town: Plymouth,          recordState: Open        },        educationPhase: 2,        priority: 0,        recordCreatedDate: 2019-03-21T13:07:25      }  ",System.Object[],{},2023,FALSE,FALSE,,0,,Year Group 12,0,,,TRUE,2023,0,,1 out of 4,N,Y,27.1,160,585,0 out of 4,2,2,2,6,5,7,0.49,0.48,0.51,0.06
32468,@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open},8,FALSE,21/03/2019,21/03/2019,,"     {        institution: {          institutionId: 35023,          displaySummary: IKB Academy 142127 (#035023),          name: IKB Academy,          laestabNo: 8004004,          ukprn: 10053995,          urn: 142127,          ucasCode: 19579,          educationPhase: 2,          educationPhaseName: Secondary,          town: Bristol,          recordState: Open        },        educationPhase: 2,        priority: 0,        recordCreatedDate: 2019-03-28T15:47:06      }  ",System.Object[],{},Unknown,FALSE,FALSE,,0,,Unknown,0,,,TRUE,,0,,,,,,,,,,,,,,,,,,
39411,@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open},9,FALSE,21/03/2019,21/03/2019,,"     {        institution: {          institutionId: 35023,          displaySummary: IKB Academy 142127 (#035023),          name: IKB Academy,          laestabNo: 8004004,          ukprn: 10053995,          urn: 142127,          ucasCode: 19579,          educationPhase: 2,          educationPhaseName: Secondary,          town: Bristol,          recordState: Open        },        educationPhase: 2,        priority: 0,        recordCreatedDate: 2019-03-28T15:47:07      }  ",System.Object[],{},Unknown,FALSE,FALSE,,0,,Unknown,0,,,TRUE,,0,,,,,,,,,,,,,,,,,,
30067,@{institutionId=22343; displaySummary=Group ABC); name=Group ABCcordState=Open},10,FALSE,21/03/2019,21/03/2019,,"     {        institution: {          institutionId: 35023,          displaySummary: IKB Academy 142127 (#035023),          name: IKB Academy,          laestabNo: 8004004,          ukprn: 10053995,          urn: 142127,          ucasCode: 19579,          educationPhase: 2,          educationPhaseName: Secondary,          town: Bristol,          recordState: Open        },        educationPhase: 2,        priority: 0,        recordCreatedDate: 2019-03-28T15:47:07      }  ",System.Object[],{},Unknown,FALSE,FALSE,,0,,Unknown,0,,,TRUE,,0,,,,,,,,,,,,,,,,,,

我到目前为止最接近的是

$rands = Get-Content ./randlist.txt;$People = ./allpeeps.csv;$csv = @();for ($i = 0; $i -lt $rands.Count; $i++){$row = "" | Select rands,People;$row.rands = $rands[$i];$row.People = $People[$i];$csv += $row};$csv | Export-Csv ./newpeeps.csv -NoTypeInformation

然而,这导致csv有2列-一个'rand'看起来是正确的,另一列叫做people,所有的信息都集中在一列中。如何将这一列添加到csv中?

这可能会有效果,因为不需要对现有的Csv做任何修改,您可以将其视为一个简单的文本文件,只需在新文件(randlist.txt)的行前加上逗号。

# the comma before `rand` is important, don't remove it!
$newData = , 'rand' + (Get-Content .randlist.txt -ReadCount 0)
$csv = Get-Content .allpeeps.csv -ReadCount 0
& {
for($i = 0; $i -lt [math]::Max($newData.Count, $csv.Count); $i++) {
# `$csv[$i].TrimStart('"')` if you need to remove the leading double-quote
$newData[$i] + ',' + $csv[$i]
}
} | Set-Content .newData.csv # Use a test file here before replacing the original Csv

相关内容

  • 没有找到相关文章

最新更新