如何在时间序列txt文件中更改日期/时间?

时间:2014-07-24 15:53:55

标签: powershell

我需要重新格式化存储在.txt文件中的时间序列数据集:

  1. 我需要将显示的时间向后移动一分钟,
  2. 我需要添加&#39 ;;'到每一行的末尾。
  3. 最好为每行执行两个格式化步骤,因为文件非常大。这里我展示了一个前后的例子:

    2013/09/30;235800;1.3526;1.3526;1.3526;1.3526;1
    2013/09/30;235900;1.3526;1.3526;1.3526;1.3526;0
    2013/10/01;000000;1.3526;1.3526;1.3526;1.3526;2
    2013/10/01;000100;1.3527;1.3527;1.3527;1.3527;20
    

    请注意午夜是否有进一步的皱纹,因为时间和日期必须退一步。

    2013/09/30;235700;1.3526;1.3526;1.3526;1.3526;1;
    2013/09/30;235800;1.3526;1.3526;1.3526;1.3526;0;
    2013/09/30;235900;1.3526;1.3526;1.3526;1.3526;2;
    2013/10/01;000000;1.3527;1.3527;1.3527;1.3527;20;
    

2 个答案:

答案 0 :(得分:1)

我会使用[DateTime]::ParseExact()[String].SubString()方法提供更短的版本。

gc $FilePath|%{
    $datetime = $_.substring(0,17).replace(";"," ")
    $newdate=[datetime]::ParseExact($datetime,"yyyy/MM/dd HHmmss",$null).AddMinutes(-1).ToString("yyyy/MM/dd HHmmss").replace(" ",";")
    "$newdate;$($_.substring(18,($_.length-18)));"
}|Out-File $NewFilePath

答案 1 :(得分:0)

[cmdletbinding()]
param([Parameter(Mandatory=$true,ValueFromPipeline=$true)]
      [string] $row)

begin
{
    [string[]] $newRows = @()
}
process
{ 
    $items = $row.Split(";")

    $date = $items[0] 

    $times = [regex]::Matches($items[1], "(\d\d)")

    [string] $cTime = $("{0}:{1}:{2}" -f $times.value)

    [datetime] $currentTime = $("{0} {1}" -f $date, $cTime)

    Write-Verbose $("Current Time: {0}" -f $currentTime)

    [datetime] $newTime = $currentTime.AddMinutes(-1)

    Write-Verbose $("NewTime: {0}" -f $newTime)

    $items[0] = $newTime.ToShortDateString()

    $items[1] = $("{0:D2}{1:D2}{2:D2}" -f $newTime.Hour, $newTime.Minute, $newTime.Second)

    [string] $newRow = ""

    foreach($item in $items)
    {
        $newRow += "{0};" -f $item
    }

    $newRows += $newRow

}

end
{
    Write-Output $newRows
}