简体   繁体   English

powershell csv 从另一列中减去一列日期值并将结果写入第三列,其中 1 天等于 24 小时

[英]powershell csv subtract one column date value from another one and write result to third where 1 day equals 24 hr

could you please help me with powershell script:你能帮我处理 powershell 脚本吗:

In CSV file I need to subtract column B date from column C and write result in column A in format 1 day = 24 hrs and same date = 12 hrs Sometimes column B can hold future values, in this case column A should contain minus (-)在 CSV 文件中,我需要从 C 列中减去 B 列日期,并将结果以 1 天 = 24 小时和同一日期 = 12 小时的格式写入 A 列有时 B 列可以保存未来值,在这种情况下,A 列应该包含减号(- )

It should look like this:它应该如下所示:

    120 11/22/2020  11/27/2020
    96  11/23/2020  11/27/2020
    72  11/24/2020  11/27/2020
    48  11/25/2020  11/27/2020
    24  11/26/2020  11/27/2020
    12  11/27/2020  11/27/2020
   -24  11/28/2020  11/27/2020
   -48  11/29/2020  11/27/2020

UPD UPD

My script have couple additional steps before subtraction.我的脚本在减法之前还有几个额外的步骤。 I'm taking raw file, TAB delimited, converting it to csv, comparing it with archived files to eliminate duplicates, removing unwanted "SrvDate" column name and replacing it with "TAT\hh" name and then exporting it to file.我正在获取原始文件,TAB 分隔,将其转换为 csv,将其与存档文件进行比较以消除重复,删除不需要的“SrvDate”列名并将其替换为“TAT\hh”名称,然后将其导出到文件。 So the only thing is missing - date comparison and fill hours in "TAT/hh" Here it is with sample data:所以唯一缺少的是 - 日期比较并在“TAT/hh”中填写小时这里是示例数据:

Script:脚本:

$in = Import-Csv (Get-ChildItem -Path E:\Test\RI\In\test\In\*.csv).Fullname -Delimiter "`t" | ? "Test Code" -in "Z620","Z630"

$in2 = Import-Csv (Get-ChildItem -Path E:\Test\RI\In\test\Arc\*.csv).Fullname

$pcomp = 'Accession'
$dup = Compare-Object $in $in2 -Property $pcomp -IncludeEqual -ExcludeDifferent -PassThru | Select-Object -ExpandProperty $pcomp
$badhead = $in | Where-Object {$_.$pcomp -notin $dup} | Select-Object *,@{Name='TAT/hh';Expression={$_.'TAT/hh'}} -Exclude 'SrvDate'
$goodhead = $badhead | Select-Object "Accession","TAT/hh","EntryDate","FinalReportDate","Patient First Name","Patient Last Name","DOB","Gender","Race","Ethnicity","Patient Address","Patient City","Patient State","Patient Zip","Patient Phone","Test Code","Test Name","Result","ClientID","Client Name","Phys  ID","Phys  Name"
if ($goodhead) {$goodhead | Where-Object {$_.$pcomp -notin $dup} | Export-Csv "E:\Test\RI\In\test\Out\Client_PtRecords_$((Get-Date).ToString("yyyyMMdd")).csv" -NoTypeInformation}

Sample data:样本数据:

Accession   SrvDate EntryDate   FinalReportDate Patient First Name  Patient Last Name   DOB Gender  Race    Ethnicity   Patient Address Patient City    Patient State   Patient Zip Patient Phone   Test Code   Test Name   Result  ClientID    Client Name Phys  ID    Phys  Name
---------   ------- ---------   --------------- ------------------  -----------------   --- ------  ----    ---------   --------------- ------------    -------------   ----------- -------------   ---------   ---------   ------  --------    ----------- --------    ----------
2132900941  NULL    11-24-2020  11-29-2020  MICHELL         PENDERGRAS      9/30/1981   F       U               35 RUSEVELT ST  PRUDENCE    RB  2909    (401)516-5642   Z620    Sars-COVID19    Positive    99984   "ASHRAF FARID, M.D."    50845   ATPAV RIGTED             
2232900942  NULL    11-25-2020  11-29-2020  MICHEL          PENDERGRA       9/30/1982   M       U               315 RUSEVELT ST PRUDENCE    RB  2909    (401)516-5643   Z620    Sars-COVID19    Positive    99984   "ASHRAF FARID, M.D."    50845   ATPAV RIGTED             
2332900943  NULL    11-26-2020  11-29-2020  MICHE           PENDERGR        9/30/1983   F       U               325 RUSEVELT ST PRUDENCE    RB  2909    (401)516-5644   Z620    Sars-COVID19    Positive    99984   "ASHRAF FARID, M.D."    50845   ATPAV RIGTED             
2432900944  NULL    11-27-2020  11-29-2020  MICH        PENDERG     9/30/1984   M       U               335 RUSEVELT ST PRUDENCE    RB  2909    (401)516-5645   Z620    Sars-COVID19    Positive    99984   "ASHRAF FARID, M.D."    50845   ATPAV RIGTED             
2532900945  NULL    11-28-2020  11-29-2020  ALLISO          JEZA            10/13/1977  F       U               15 KAUTEEKEE AVE    SOUTH PRUDENCE  RB  2911    (908)930-9213   Z620    Sars-COVID19    Positive    99984   "ASHRAF FARID, M.D."    50845   ATPAV RIGTED             
2632900946  NULL    11-29-2020  11-29-2020  ALLIS           JEZ             10/13/1978  M       U               151 KAUTEEKEE AVE   SOUTH PRUDENCE  RB  2911    (908)930-9214   Z620    Sars-COVID19    Positive    99984   "ASHRAF FARID, M.D."    50845   ATPAV RIGTED             
2732900947  NULL    11-30-2020  11-29-2020  ALLI            JE              10/13/1979  F       U               152 KAUTEEKEE AVE   SOUTH PRUDENCE  RB  2911    (908)930-9215   Z620    Sars-COVID19    Positive    99984   "ASHRAF FARID, M.D."    50845   ATPAV RIGTED             
2832900948  NULL    12-1-2020   11-29-2020  ALL         J           10/13/1980  M       U               153 KAUTEEKEE AVE   SOUTH PRUDENCE  RB  2911    (908)930-9216   Z620    Sars-COVID19    Positive    99984   "ASHRAF FARID, M.D."    50845   ATPAV RIGTED

UPD UPD

I used Алексей's script and insert it in my, but I've faced problem to combine results from two different calculations.我使用了 Алексей 的脚本并将其插入到我的脚本中,但我遇到了将两个不同计算的结果结合起来的问题。

In my script - I'm getting results with correct headers, searched elements only, exporting only if there are results and without duplicates in $goodhead.在我的脚本中 - 我得到的结果具有正确的标题,仅搜索元素,仅在有结果且 $goodhead 中没有重复项时才导出。

In Алексей's script I'm getting calculations for TurnAround Time in $tat (I've selected calculated column only)在 Алексей 的脚本中,我正在计算 $tat 中的 TurnAround Time(我只选择了计算列)

Is there a way to fill in $tat into $goodhead?有没有办法将 $tat 填入 $goodhead?

Here is my updated code:这是我更新的代码:

$in = Import-Csv (Get-ChildItem -Path E:\Test\RI\In\test\In\*.csv).Fullname -Delimiter "`t" | ? "Test Code" -in "Z620","Z630"

$in2 = Import-Csv (Get-ChildItem -Path E:\Test\RI\In\test\Arc\*.csv).Fullname

$pcomp = 'Accession'
$dup = Compare-Object $in $in2 -Property $pcomp -IncludeEqual -ExcludeDifferent -PassThru | Select-Object -ExpandProperty $pcomp
$badhead = $in | Where-Object {$_.$pcomp -notin $dup} | Select-Object *,@{Name='TAT/hh';Expression={$_.'TAT/hh'}} -Exclude 'SrvDate'
$goodhead = $badhead | Select-Object "Accession","TAT/hh","EntryDate","FinalReportDate","Patient First Name","Patient Last Name","DOB","Gender","Race","Ethnicity","Patient Address","Patient City","Patient State","Patient Zip","Patient Phone","Test Code","Test Name","Result","ClientID","Client Name","Phys ID","Phys Name"

$tatin = $goodhead | Select-Object * | % {
    $EntryDate = [datetime]::parseexact($_.EntryDate, 'yyyy-MM-dd', $null)
    $FinalReportDate = [datetime]::parseexact($_.FinalReportDate, 'yyyy-MM-dd', $null)

    $tatcalc = switch (($FinalReportDate - $EntryDate).TotalDays){
        0 {12}
        default {$_ * 24}
    }
    [PSCustomObject]@{
        'TAT/hh' = $tatcalc
        EntryDate = $EntryDate
        FinalReportDate = $FinalReportDate
    }

}

$tat = $tatin | Select "TAT/hh"

if ($tat) {$tat | Sort-Object "Result" -Descending | Export-Csv "E:\Test\RI\In\test\Out\Client_PtRecords_$((Get-Date).ToString("yyyyMMdd")).csv" -NoTypeInformation}

Thank you!谢谢!

PS I'm just a beginner with Powershell, trying to learn it on daily tasks. PS我只是Powershell的初学者,试图在日常任务中学习它。

$in = Import-Csv (Get-ChildItem -Path D:\tmp\alex\Client_PtRecords_20201203.csv).Fullname -Delimiter "`t" | ? "Test Code" -in "Z620","Z630"

$in2 = Import-Csv (Get-ChildItem -Path D:\tmp\alex\arc\Client_PtRecords_20201202.csv).Fullname

$pcomp = 'Accession'
$dup = Compare-Object $in $in2 -Property $pcomp -IncludeEqual -ExcludeDifferent -PassThru | Select-Object -ExpandProperty $pcomp
$badhead = $in | Where-Object {$_.$pcomp -notin $dup} | Select-Object *,@{Name='TAT/hh';Expression={$_.'TAT/hh'}} -Exclude 'SrvDate'
$goodhead = $badhead | Select-Object "Accession", @{name = "TAT/hh"; expression = {switch(([datetime]::parseexact($_.FinalReportDate, 'yyyy-MM-dd', $null)-[datetime]::parseexact($_.EntryDate, 'yyyy-MM-dd', $null)).TotalDays){ 0 {12}; default {$_ * 24}}}},"EntryDate","FinalReportDate","Patient First Name","Patient Last Name","DOB","Gender","Race","Ethnicity","Patient Address","Patient City","Patient State","Patient Zip","Patient Phone","Test Code","Test Name","Result","ClientID","Client Name","Phys ID","Phys Name"

$tatin = $goodhead | Select-Object * 

$tat = $tatin | Select "TAT/hh"

if ($tat) {$tat | Sort-Object "Result" -Descending | Export-Csv "D:\tmp\alex\arc\Client_PtRecords_$((Get-Date).ToString("yyyyMMdd")).csv" -NoTypeInformation}

暂无
暂无

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

相关问题 使用Powershell将整个列从一个CSV文件复制到另一个 - Copying an entire column from one CSV file to another using Powershell Powershell从可变日期减去1天 - Powershell Subtract 1 day from variable date 从一个 CSV 创建多个文件,从列中的每个值创建一个文件 [Powershell] - Create multiple files from one CSV, one files from each value in a column [Powershell] Powershell - 将一个 CSV 中的数据添加到另一个 CSV 文件的新列和行中 - Powershell - Add data from one CSV into new column and rows of another CSV file PowerShell:列出 CSV 文件行,其中第三列和最后一列之间至少有一个值等于“0”或“1” - PowerShell: list CSV file rows where at least one value between the 3rd and last column is equal to “0” or “1” Powershell: how to retrieve powershell commands from a csv and execute one by one, then output the result to the new csv - Powershell: how to retrieve powershell commands from a csv and execute one by one, then output the result to the new csv Powershell-比较两个csv行并将结果写入第三行 - Powershell - Compare two csv rows and write the result in the third 如何使用 powershell 将内容从一个 CSV 复制到另一个 CSV? - How to copy contents from one CSV into another CSV with powershell? PowerShell - 尝试根据列值将 2 个 CSV 文件中的数据合并为一个 - PowerShell - Trying to combine data from 2 CSV files into one based on column value 使用 PowerShell 将列从一个 csv 文件复制到另一个文件 - Copy columns from one csv file to another using PowerShell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM