CleanCode PowerShell Libraries v1.2.08 API: CleanCode » FileTools » ConvertFrom-Text
$records = Get-Content .\FixedWidth.log | ConvertFrom-Text -Pattern $regex-or-
$records = ConvertFrom-Text -InputObject $myDataArray -Pattern $regex
$records = $data -split "`r`n" | ConvertFrom-Text -pattern $regex
(?<field name goes here>.{field length goes here})
(?<n1>.{l1})(?<n2>.{l2})(?<n3>.{l3})
^(?<n1>.{l1})(?<n2>.{l2})(?<n3>.{l3})$
> Anchors, RequireAll=$false: matches entire line, non-matches ignored
> Anchors, RequireAll=$true: matches entire line, non-matches error
> No anchors, RequireAll=$false: matches substring, non-matches ignored
> No anchors, RequireAll=$true: matches substring, non-matches error
Data to import.
Required? true
Position? 1
Default value
Accept pipeline input? true (ByValue, ByPropertyName)
Accept wildcard characters? false
Regular expression to match input lines.
The pattern must provide one or more named capture groups--see the examples
for details.
This must be a regular expression either as a [string] or a [regex].
The latter allows including option flags in the expression.
Required? true
Position? 2
Default value
Accept pipeline input? false
Accept wildcard characters? false
Requires all lines in the file to match the supplied regular expression;
otherwise, throws an exception.
If omitted or set to false, non-matching lines are silently ignored.
Required? false
Position? named
Default value False
Accept pipeline input? false
Accept wildcard characters? false
Specifies whether records span multiple lines; default is a single line per record.
Required? false
Position? named
Default value False
Accept pipeline input? false
Accept wildcard characters? false
This cmdlet supports the common parameters: Verbose, Debug,
ErrorAction, ErrorVariable, WarningAction, WarningVariable,
OutBuffer and OutVariable. For more information, see
about_CommonParameters (http://go.microsoft.com/fwlink/?LinkID=113216).
This function is part of the CleanCode toolbox
from http://cleancode.sourceforge.net/.
Since CleanCode 1.2.02
^(?<n1>.{l1})(?<n2>.{l2})(?<n3>.{l3})$
$regex = "^(?<FirstName>.{7})(?<LastName>.{10})(?<Id>.{3})$"
--------------------------
12345671234567890123
george jetson 5
warren buffett 123
horatioalger -99
--------------------------
Id FirstName LastName
-- --------- --------
123 1234567 1234567890
5 george jetson
123 warren buffett
-99 horatio alger
$regex = "^(?<FirstName>.{7})(?<LastName>.{10})(?<Id>.{3})$"
$data = @"
12345671234567890123
george jetson 5
warren buffett 123
horatioalger -99
"@
$regex = "^(?<FirstName>.{7})(?<LastName>.{10})(?<Description>.*)$"
--------------------------
12345671234567890123
george jetson arbitrary text here
warren buffett stuff
horatioalger more stuff
--------------------------
--------------------------
127.0.0.1 - frank [10/Oct/2012:13:55:36 -0700] "GET /apache_pb.gif HTTP/1.0" 200 2326
111.111.111.111 - martha [18/Oct/2012:01:17:44 -0700] "GET / HTTP/1.0" 200 101
111.111.111.111 - - [18/Oct/2007:11:17:55 -0700] "GET /style.css HTTP/1.1" 200 4525
--------------------------
Host or IP address 127.0.0.1
Remote log name -
Authenticated user name frank
Timestamp [10/Oct/2000:13:55:36 -0700]
Access request GET /apache_pb.gif HTTP/1.0
Result status code 200
Bytes transferred 2326
$apacheExtractor = "(?<Host>\S*)",
"(?<LogName>.*?)",
"(?<UserId>\S*)",
"\[(?<TimeStamp>.*?)\]",
"`"(?<Request>[^`"]*)`"",
"(?<Status>\d{3})",
"(?<BytesSent>\S*)" -join "\s+"
TimeStamp LogName Host UserId Status Request BytesSent
--------- ------- ---- ------ ------ ------- ---------
10/Oct/2012:13:55:36 -0700 - 127.0.0.1 frank 200 GET /apache_pb.gif HTTP/1.0 2326
18/Oct/2012:01:17:44 -0700 - 111.111.111.111 martha 200 GET / HTTP/1.0 101
18/Oct/2007:11:17:55 -0700 - 111.111.111.111 - 200 GET /style.css HTTP/1.1 4525
$data=@'
DC Options: IS_GC
DC=company,DC=org
BLL\045ADDC001 via RPC
DC object GUID: 26446473-3433-4c73-942d-c750f0e476ec
Last attempt @ 2007-08-21 13:38:53 was successful.
CN=Configuration,DC=company,DC=org
BLL\045ADDC001 via RPC
DC object GUID: 26446473-3433-4c73-942d-c750f0e476ec
Last attempt @ 2007-08-21 13:38:53 was successful.
CN=Schema,CN=Configuration,DC=company,DC=org
BLL\045ADDC001 via RPC
DC object GUID: 26446473-3433-4c73-942d-c750f0e476ec
Last attempt @ 2007-08-21 13:38:54 was successful.
'@
$regex = [regex] '(?msx)
^ (?<partition> (CN|DC)=[^$]+?)\s*$
.+? # skip intervening
(?<Site> \w+) \\ (?<DC> \w+)
.+?
Last\ attempt\D+ (?<date> [\d\-]+\ [\d\:]+ )
'
PS> Get-Content data.txt | Out-String | ConvertFrom-Text -pattern $regex
-none-
This documentation set was created with CleanCode's DocTreeGenerator.