我想按当前行中的 "full" 或 "incr" 或 "manual" 中的任何一个进行拆分

I want to split by either word "full" or "incr" or "manual" whichever is in current line

我有如下所示的线

rspedw03.corpads.local 3085876532 JC_UNIX_FS01_INCR_DD2 JC_FS_DD2 UNIX_FS01_INCR_DD2 incr 02/23/2022 03/29/2022 03/29/2022  disk 1645592426 backup 3013 MB JCBDD2301P.CORPADS.LOCAL
rsuedw01.corpads.local 1020344 JC_DB_DB2 JC_DB Clone_DR    full 02/23/2022 04/04/2022 04/04/2022         disk 1645592431 Clone_DR    997 KB MNBDD3302P.corpads.local
rsuedw01.corpads.local 1020344 JC_DB_DB2                   full 02/23/2022 04/04/2022 03/30/2022         disk 1645592431             997 KB JCBDD1300P.corpads.local
rsuedw03.corpads.local 12608 MN_UNIX_NP_7_Days MN_DB Clone_DR full 02/23/2022 04/21/2022 04/21/2022      disk 1645592432 Clone_DR     13 KB JCBDD1300P.corpads.local

我想按当前行中的单词“full”或“incr”或“manual”进行拆分。基本上我想要下面行中的第二个日期

rspedw02.corpads.local 4829860108 JC_UNIX_FS01_INCR_DD2 JC_FS_DD2 UNIX_FS01_INCR_DD2 incr 02/23/2022 03/29/2022 03/26/2022  disk 1645592421 Clone_DR 4716 MB MNBDD5304P.CORPADS.LOCAL
rspedw02.corpads.local 4829860108 JC_UNIX_FS01_INCR_DD2 JC_FS_DD2 incr 02/23/2022 03/29/2022 03/28/2022  disk 1645592421 backup 4716 MB JCBDD2301P.CORPADS.LOCAL
rspedw03.corpads.local 3085876532 JC_UNIX_FS01_INCR_DD2 full 02/23/2022 03/29/2022 03/22/2022  disk 1645592426 Clone_DR 3013 MB MNBDD5304P.CORPADS.LOCAL
rspedw03.corpads.local 3085876532 JC_UNIX_FS01_INCR_DD2 JC_FS_DD2 UNIX_FS01_INCR_DD2 manual 02/23/2022 03/29/2022 03/29/2022  disk 1645592426 backup 3013 MB JCBDD2301P.CORPADS.LOCAL

感兴趣的单词后的第二个日期是第二个whitespace-separated标记;而不是 -split approach, which would require multiple steps, you can use a single -replace 操作来提取它:

# Sample array of input lines.
$lines = @'
rspedw03.corpads.local 3085876532 JC_UNIX_FS01_INCR_DD2 JC_FS_DD2 UNIX_FS01_INCR_DD2 incr 02/23/2022 03/29/2022 03/29/2022  disk 1645592426 backup 3013 MB JCBDD2301P.CORPADS.LOCAL
rsuedw01.corpads.local 1020344 JC_DB_DB2 JC_DB Clone_DR    full 02/23/2022 04/04/2022 04/04/2022         disk 1645592431 Clone_DR    997 KB MNBDD3302P.corpads.local
rsuedw01.corpads.local 1020344 JC_DB_DB2                   full 02/23/2022 04/04/2022 03/30/2022         disk 1645592431             997 KB JCBDD1300P.corpads.local
rsuedw03.corpads.local 12608 MN_UNIX_NP_7_Days MN_DB Clone_DR full 02/23/2022 04/21/2022 04/21/2022      disk 1645592432 Clone_DR     13 KB JCBDD1300P.corpads.local
'@ -split '\r?\n'

# Replace $lines with (Get-Content $yourFile) to read from a file
$lines -creplace '^.+ (?:full|incr|manual) .+? (.+?) .*', ''

注意 -creplace 的使用,它是 -replace 运算符的大小写 敏感 变体。

regex and the ability to experiment with it, see this regex101.com page的解释。

输出:

03/29/2022
04/04/2022
04/04/2022
04/21/2022

我会使用正则表达式。这个正则表达式可能不够严格,也可能不够严格,但这是一个开始。它采用了一种非最佳的特定日期格式。

Get-Content -Path .\spl.txt |
    Foreach-Object {
        if ($_ -match '.*( full | incr | manual )[\d/]{10} ([\d/]{10}).*') {
            $Matches[2]
        }
    }