Home > Software engineering >  I want to split by either word "full" or "incr" or "manual" whichever
I want to split by either word "full" or "incr" or "manual" whichever

Time:03-26

I have line as shown below

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

I want to split by either word "full" or "incr" or "manual" whichever is in current line. Basically i want 2nd date from below lines

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

CodePudding user response:

The second date after the word of interest is the second whitespace-separated token; instead of a -split approach, which would require multiple steps, you can use a single -replace operation to extract it:

# 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) . ? (. ?) .*', '$1'

Note the use of -creplace, the case-sensitive variant of the -replace operator.

For an explanation of the regex and the ability to experiment with it, see this regex101.com page.

Output:

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

CodePudding user response:

I would use a regex. This regex might or might not be stringent enough, but this is a start. It assumes a specific date format which is not optimal.

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