首页 > 解决方案 > 如何根据 csv 列值生成不同的 ID?

问题描述

我需要为“计划”列中的值生成一个 ID,并为“脚本或预期文件”列中的值生成另一个 ID。

“Schedule”列仅包含几个小时的模式(\d):h(\d+),而另一列包含“技术”字符或模式的字符串^(.{4})-^(.{4}).

如果类型是“TechnologyInteraction 或 TechnologyService 或 TechnologyEvent”,我的目标是创建一个不同的 id。我可以创建 TechnologyInteraction 或 TechnologyService 类型的对象,但不能创建“TechnologyEvent”。

拜托,我需要帮助才能得到这样的输出:

ID,"Type","Name","Documentation"
79570056-ab4f-6969-8c58-a5bd5847a895,"TechnologyInteraction","WEBX965H","RMNF - xWEBX1"
b57a5fff-9dd2-4cm1-9141-04c042f49498,"TechnologyService","WEBX965H-005S","Lancement Sauv. RMAN"
id-bdca3a1b39ed49cd80ae41deaa88094c,"TechnologyEvent","Lancement Sauv. RMAN  05:00",""

起始 CSV 是:

Jobstream,Jobstream Description,Op num, Job,Script or expected file(s), Server, user,location,Job Description,Planification
PAXCWEBX965H,"RMNF - xWEBX1","9","","technical","","","","Begin Of JobStream","05h00"
PAXCWEBX965H,"RMNF - xWEBX1","40","PWEBX0GJ","PWEBX-965H-005S.KSH","PRAXCWBXLBDD01","svc_bddo_user","F+WEBX-027","Lancement Sauv. RMAN","05h00"
function newElements{

    process{
    
        # import data 
    $data = Import-csv -Path $env:USERPROFILE\A.csv -Delimiter ';'

        $NewExtract_AGRe_TWS_ALL_20200925= ForEach($Entry in $ElementCsv){
  

 
    # -or [string]::IsNullOrEmpty($Entry."Script or expected file(s)")
    if ( ($Entry."Script or expected file(s)" -ilike 'technical') -or [string]::IsNullOrEmpty($Entry."Script or expected file(s)")  ) {
        $Entry."Jobstream"=$Entry."Jobstream" -replace '^(?:PAXC)?(.+?)','$1' 

    }

    else {
        $Entry."Jobstream" = $Entry.'Script or expected file(s)' -replace '^(?:P|A|X|C)?(.+?)(\.(?:BAT|KSH)|$)','$1' -replace '^(.{4})-','$1'
    }

        $Entry
    }
        # Export Extract_AGRe_TWS_ALL_20211005.csv  in new B.csv file 
        $NewExtract_AGRe_TWS_ALL_20200925  | Export-Csv $env:USERPROFILE\B.csv -NoTypeInformation -Encoding UTF8
    
        $NewExtract_AGRe_TWS_ALL_20200925Jobstream= Import-Csv $env:USERPROFILE\B.csv | Where { $oldElementsCsv.Name -notcontains $_.Jobstream}| Export-Csv $env:USERPROFILE\C.csv -NoTypeInformation  -Encoding UTF8 
        $ImportCsv=Import-Csv $env:USERPROFILE\C.csv
        #Output progress:
        $output= @()
        #Jobstream, Jobstream Description,Op num, Job,Script or expected file(s),Server, user, location, Job Description
        ForEach ($column in $ImportCsv){
        $pattern='^(.*)-'
        $pattern2='^(.*)_'
    if($column.Jobstream  -notmatch $pattern){

        $output1= New-Object PsObject -Property @{"ID"=[guid]::NewGuid().ToString(); "Type"="TechnologyInteraction"; "Name"= $column.Jobstream; "Documentation"=$column."Job Description" + " `r`nSever: $($column.Server)  `r`nuser: $($column.user) " ;  "Planification"= $column.Planification }   
        Write-Warning "Found new Jobstream : $($column.Jobstream)"
        Write-Warning "Found new Jobstream Description : $($column."Job Description")"
        $output= $output + $output1
    }  

  
          elseif($column.Jobstream  -match $pattern2) 
    {

        $output2= New-Object PsObject -Property @{"ID"=[guid]::NewGuid().ToString(); "Type"="TechnologyService"; "Name"=$column.Jobstream; "Documentation"= $column."Job Description"+ " `r`nSever: $($column.Server)  `r`nuser: $($column.user) " ; "Planification"= $column.Planification   } 
        Write-Warning "New Jobstream : $($column.Jobstream)"
        Write-Warning "New Jobstream Description : $($column."Job Description")"
        $output= $output + $output2

    } 
   elseif (![string]::IsNullOrEmpty($column.Planification) ){

        $output4= New-Object PsObject -Property @{"ID"=[guid]::NewGuid().ToString(); "Type"="TechnologyEvent"; "Name"= $column."Job Description" + " " + $column.Planification ; "Documentation"= ""  } 

        Write-Warning "New Type TechnologyEvent : $( $column."Job Description")"
        Write-Warning "New Type TechnologyEvent Description : $($column."Job Description")"
        $output= $output + $output4

    }
    else{

        $output3= New-Object PsObject -Property @{"ID"=[guid]::NewGuid().ToString(); "Type"="TechnologyService"; "Name"= $column.Jobstream; "Documentation"= $column."Job Description" + " " + $column.Planification + " `r`nSever: $($column.Server)  `r`nuser: $($column.user)" } 

        Write-Warning "New Jobstream : $($column.Jobstream)"
        Write-Warning "New Jobstream Description : $($column."Job Description")"
        $output= $output + $output3
    }

 

    }
    $output |Select-Object -Property "ID","Type","Name","Documentation" -Unique| Export-Csv $path\D.csv -NoTypeInformation -Encoding UTF8 

    #open new File:
    notepad $path\D.csv
    Write-Host "Ending newElements "
    }
}newElements

标签: powershellcsv

解决方案


    function newElements{

    process{
    
        # import data 
    $data = Import-csv -Path $env:USERPROFILE\A.csv -Delimiter ';'

        $NewExtract_AGRe_TWS_ALL_20200925= ForEach($Entry in $ElementCsv){
  

 
    # -or [string]::IsNullOrEmpty($Entry."Script or expected file(s)")
    if ( ($Entry."Script or expected file(s)" -ilike 'technical') -or [string]::IsNullOrEmpty($Entry."Script or expected file(s)")  ) {
        $Entry."Jobstream"=$Entry."Jobstream" -replace '^(?:PAXC)?(.+?)','$1' 

    }

    else {
        $Entry."Jobstream" = $Entry.'Script or expected file(s)' -replace '^(?:P|A|X|C)?(.+?)(\.(?:BAT|KSH)|$)','$1' -replace '^(.{4})-','$1'
    }

        $Entry
    }
        # Export Extract_AGRe_TWS_ALL_20211005.csv  in new B.csv file 
        $NewExtract_AGRe_TWS_ALL_20200925  | Export-Csv $env:USERPROFILE\B.csv -NoTypeInformation -Encoding UTF8
    
        $NewExtract_AGRe_TWS_ALL_20200925Jobstream= Import-Csv $env:USERPROFILE\B.csv | Where { $oldElementsCsv.Name -notcontains $_.Jobstream}| Export-Csv $env:USERPROFILE\C.csv -NoTypeInformation  -Encoding UTF8 
        $ImportCsv=Import-Csv $env:USERPROFILE\C.csv
        #Output progress:
        $output= @()
        #Jobstream, Jobstream Description,Op num, Job,Script or expected file(s),Server, user, location, Job Description
        ForEach ($column in $ImportCsv){
        $pattern='^(.*)-'
        $pattern2='^(.*)_'
    if($column.Jobstream  -notmatch $pattern){

        $output1= New-Object PsObject -Property @{"ID"=[guid]::NewGuid().ToString(); "Type"="TechnologyInteraction"; "Name"= $column.Jobstream; "Documentation"=$column."Job Description" + " `r`nSever: $($column.Server)  `r`nuser: $($column.user) " ;  "Planification"= $column.Planification }   
        Write-Warning "Found new Jobstream : $($column.Jobstream)"
        Write-Warning "Found new Jobstream Description : $($column."Job Description")"
        $output= $output + $output1
    }  

  
          elseif($column.Jobstream  -match $pattern2) 
    {

        $output2= New-Object PsObject -Property @{"ID"=[guid]::NewGuid().ToString(); "Type"="TechnologyService"; "Name"=$column.Jobstream; "Documentation"= $column."Job Description"+ " `r`nSever: $($column.Server)  `r`nuser: $($column.user) " ; "Planification"= $column.Planification   } 
        Write-Warning "New Jobstream : $($column.Jobstream)"
        Write-Warning "New Jobstream Description : $($column."Job Description")"
        $output= $output + $output2

    } 
   elseif (![string]::IsNullOrEmpty($column.Planification) ){

        $output4= New-Object PsObject -Property @{"ID"=[guid]::NewGuid().ToString(); "Type"="TechnologyEvent"; "Name"=$column.Planification ; "Documentation"= ""  } 

        Write-Warning "New Type TechnologyEvent : $( $column."Job Description")"
        Write-Warning "New Type TechnologyEvent Description : $($column."Job Description")"
        $output= $output + $output4

    }
    else{

        $output3= New-Object PsObject -Property @{"ID"=[guid]::NewGuid().ToString(); "Type"="TechnologyService"; "Name"= $column.Jobstream; "Documentation"= $column."Job Description" + " " + $column.Planification + " `r`nSever: $($column.Server)  `r`nuser: $($column.user)" } 

        Write-Warning "New Jobstream : $($column.Jobstream)"
        Write-Warning "New Jobstream Description : $($column."Job Description")"
        $output= $output + $output3
    }

 

    }
    $output |Select-Object -Property "ID","Type","Name","Documentation" -Unique| Export-Csv $path\D.csv -NoTypeInformation -Encoding UTF8 

    #open new File:
    notepad $path\D.csv
    Write-Host "Ending newElements "
    }
}newElements
powershell
csv

推荐阅读