我建议将其解析为带有列标题的漂亮 CSV 文件:
# read the file as single multilined string and split on `PIPELINE REF`
$result = (Get-Content -Path '.\bolt-out.bom' -Raw) -split 'PIPELINE REF' | Where-Object {$_ -match '\S' } | ForEach-Object {
# split the part in separate lines and trim to get rid of all leading and trailing spaces
$lines = ($_ -split '\r?\n').Trim()
$lines[0] = 'PIPELINE REF ' + $lines[0] # re-add 'PIPELINE REF'
# create an empty item
$item = '' | Select-Object LineRef,PipingSpec,Description,NS,Length,ItemCode,QTY_Fab,QTY_Erec,Total_QTY
# parse line by line
switch -Regex ($lines) {
'^PIPELINE REF (\w+)' { $item.LineRef = $matches[1] }
'^PIPING SPEC (\w+)' { $item.PipingSpec = $matches[1] }
'^(\d+.*)\s{2,}([^\s]+)\s{2,}([^\s]+)\s{2,}([^\s]+)\s{2,}([^\s]+)\s{2,}([^\s]+)' {
$item.Description = $matches[1]
$item.NS = $matches[2]
$item.Length = $matches[3]
$item.ItemCode = $matches[4]
$item.QTY_Fab = [int]$matches[5]
$item.QTY_Erec = [int]$matches[6]
$item.Total_QTY = [int]$matches[5] + [int]$matches[6]
}
}
# output the item
$item
}
# output on screen
$result
# output to comma separated CSV file (WITH headers)
$result | Export-Csv -Path '.\boltsandnuts.csv' -NoTypeInformation
屏幕上的输出应如下所示:
LineRef : LINENUMBER_A
PipingSpec : 01B
Description : 70 STUD BOLTS & 2 HEAVY Hex Nuts
NS : 1/2
Length : 70
ItemCode : 0597461
QTY_Fab : 0
QTY_Erec : 4
Total_QTY : 4
正则表达式长行的正则表达式详细信息:
^ Assert position at the beginning of the string
( Match the regular expression below and capture its match into backreference number 1
\d Match a single digit 0..9
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
. Match any single character that is not a line break character
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
)
\s Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
{2,} Between 2 and unlimited times, as many times as possible, giving back as needed (greedy)
( Match the regular expression below and capture its match into backreference number 2
[^\s] Match any character that is NOT a “A whitespace character (spaces, tabs, line breaks, etc.)”
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
\s Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
{2,} Between 2 and unlimited times, as many times as possible, giving back as needed (greedy)
( Match the regular expression below and capture its match into backreference number 3
[^\s] Match any character that is NOT a “A whitespace character (spaces, tabs, line breaks, etc.)”
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
\s Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
{2,} Between 2 and unlimited times, as many times as possible, giving back as needed (greedy)
( Match the regular expression below and capture its match into backreference number 4
[^\s] Match any character that is NOT a “A whitespace character (spaces, tabs, line breaks, etc.)”
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
\s Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
{2,} Between 2 and unlimited times, as many times as possible, giving back as needed (greedy)
( Match the regular expression below and capture its match into backreference number 5
[^\s] Match any character that is NOT a “A whitespace character (spaces, tabs, line breaks, etc.)”
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
\s # Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
{2,} # Between 2 and unlimited times, as many times as possible, giving back as needed (greedy)
( # Match the regular expression below and capture its match into backreference number 6
[^\s] # Match any character that is NOT a “A whitespace character (spaces, tabs, line breaks, etc.)”
+ # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)