只是powershell的新手。我需要一份从下面的txt报告中提取的列表报告。这只是一些例子。我有一万多条这样的线路。
所期望的逗号分隔的结果如下所示:LINENUMBER_A,01B,双头螺栓和;2个重型六角螺母,1/2,700597461,0,4LINENUMBER_B,01C,双头螺栓和;2个重型六角螺母,5/8,907452665,0,16LINENUMBER_B,01C,双头螺栓和;3个HH螺母,1.1/820989993,0,16
我试过
$p = @("PIPELINE","STUD BOLT") Get-Content '.bolt-out.bom' | Select-String -Pattern $p -SimpleMatch | Set-Content test0.txt
但是结果并不像预期的那样。任何人都可以帮我感激。
结果如下。我可能只需要第一个数组查找+第二个查找的连接,而不是新的行号。
PIPELINE REF LINENUMBER_A 90 STUD BOLTS & 2 HEAVY Hex Nuts 5/8 90 7452665 0 16
我建议将其解析为一个带有列标题的漂亮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
Regex长正则表达式行的详细信息:
^ 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)
)