RegEx & Powershell: use Regex to create files based on the name found in CSV

I am trying to import and process CSV. From position 2-4 of the third column, I want to write this line to a file with this identifier.

As an example, from line 1 for "results.csv" I try to write the corresponding line to a file named "274.txt":

10.121.8.84, TRUE, N 274 SECX9010C5D, 3/20/2015 15:48

I am somewhat sure that my regex may be wrong. It looks great and is capable of viewing sample text in Expresso. However, when it is added to the script, I do not see any results.

Results.csv:

10.121.88.84,TRUE,N274SECX9610C5D,3/20/2015 15:48
10.77.89.109,TRUE,L186OFFX2K6KMX1,3/24/2015 9:49
10.144.18.135,TRUE,N488FOOD2NK6MB1,3/25/2015 7:20
10.181.92.175,FALSE,,
10.147.86.54,FALSE,,

Powershell Code:

$path = Split-Path -parent $MyInvocation.MyCommand.Definition
$Results_File = $path + "\results.csv"

Import-Csv $Results_File | ForEach {
    If ($_ -Match '^.*,\w(?<filename>\d{3}).*') {
        $_ | Out-File -Append "$($Matches.Filename).csv"
    }
}
+4
source share
1

Import-Csv Get-Content

$path = Split-Path -parent $MyInvocation.MyCommand.Definition
$Results_File = $path + "\results.csv"

get-content $Results_File | ForEach {
    If ($_ -Match '^.*,\w(?<filename>\d{3}).*') {
        $_ | Out-File -Append "$($Matches.Filename).csv"
    }
}

Import-Csv alist , . :

Import-Csv $Results_File -Header 'C1','C2','C3','C4'
C1            C2    C3              C4                                                  
--            --    --              --                                                  
10.121.88.84  TRUE  N274SECX9610C5D 3/20/2015 15:48                                     
10.77.89.109  TRUE  L186OFFX2K6KMX1 3/24/2015 9:49                                      
10.144.18.135 TRUE  N488FOOD2NK6MB1 3/25/2015 7:20                                      
10.181.92.175 FALSE                                                                     
10.147.86.54  FALSE 

$_.C3 RegEx.

+4

Source: https://habr.com/ru/post/1605765/


All Articles