有分离功能

时间:2019-02-02 12:08:36

标签: powershell

我一直在寻找要分离的功能,但发现-split,但不知道如何应用。我想将此文件分为4部分(结果),但是我不知道该怎么做。

192_168_249_1_01_22_2019_01_38_55.txt

我希望您拥有的结果

192.168.246.1
22-22-2019
01:38:55
.TXT

2 个答案:

答案 0 :(得分:1)

另一种方法是使用正则表达式来获取文件名的不同部分。
也许是这样的:

function Split-FileName {
    param ([string]$FileName)
    if ($FileName -match '^(?<ip>(?:\d{1,3}_){3}\d{1,3})_(?<date>(?:\d{1,2}_){2}\d{4})_(?<time>(?:\d{1,2}_){2}\d{1,2})(?<ext>\.\w+)') {
        [PSCustomObject]@{
            IPAddress = $matches['ip'] -replace '_', '.'
            Date =      $matches['date'] -replace '_', '-'
            Time =      $matches['time'] -replace '_', ':'
            Extension = $matches['ext'].ToUpper()
        }
    }
}

像这样使用它:

Split-FileName '192_168_249_1_01_22_2019_01_38_55.txt'

以所需的格式(默认为表格)返回具有全部4个部分的对象

IPAddress     Date       Time     Extension
---------     ----       ----     ---------
192.168.249.1 01-22-2019 01:38:55 .TXT

或列出(如果有)

Split-FileName '192_168_249_1_01_22_2019_01_38_55.txt' | Format-List
IPAddress : 192.168.249.1
Date      : 01-22-2019
Time      : 01:38:55
Extension : .TXT

正则表达式详细信息

^                 Assert position at the beginning of a line (at beginning of the string or after a line break character)
(?<ip>            Match the regular expression below and capture its match into backreference with name “ip”
   (?:            Match the regular expression below
      \d          Match a single digit 0..9
         {1,3}    Between one and 3 times, as many times as possible, giving back as needed (greedy)
      _           Match the character “_” literally
   ){3}           Exactly 3 times
   \d             Match a single digit 0..9
      {1,3}       Between one and 3 times, as many times as possible, giving back as needed (greedy)
)
_                 Match the character “_” literally
(?<date>          Match the regular expression below and capture its match into backreference with name “date”
   (?:            Match the regular expression below
      \d          Match a single digit 0..9
         {1,2}    Between one and 2 times, as many times as possible, giving back as needed (greedy)
      _           Match the character “_” literally
   ){2}           Exactly 2 times
   \d             Match a single digit 0..9
      {4}         Exactly 4 times
)
_                 Match the character “_” literally
(?<time>          Match the regular expression below and capture its match into backreference with name “time”
   (?:            Match the regular expression below
      \d          Match a single digit 0..9
         {1,2}    Between one and 2 times, as many times as possible, giving back as needed (greedy)
      _           Match the character “_” literally
   ){2}           Exactly 2 times
   \d             Match a single digit 0..9
      {1,2}       Between one and 2 times, as many times as possible, giving back as needed (greedy)
)
(?<ext>           Match the regular expression below and capture its match into backreference with name “ext”
   \.             Match the character “.” literally
   \w             Match a single character that is a “word character” (letters, digits, etc.)
      +           Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)

答案 1 :(得分:0)

您可以尝试使用如下功能:

function get-split {
    param([string]$text)
    ($text.Split("_"))[0..3] -join "."
    ($text.Split("_"))[4..6] -join "-"
    (($text.Split("_"))[7..9] -join ":").split(".")[0]
    ".$((($text.Split("_"))[7..9] -join ":").split(".")[1])"
}

我知道它可以改进,但是我认为可以...