以这个令人敬畏的答案https://stackoverflow.com/a/58815537/11076819考虑我的上一个问题 一旦有了这种格式的文本文件,我就会遇到问题。
Configuration
;
; Authors: James
; Created: 10/11/2018
;
; Accepted
Name
James
Class
A2
Birthday
1 September 1982
Family Member
4
First Year Salary Number
100 USD
Second Year Salary Number
150 USD
Company Name
Unlimited Company
ExpectedSalaryNumber
FY:350 USD
SY:450 USD
TS:2000 USD
在ExpectedSalaryNumber
中,由于空白是
FY:350 USD
SY:450 USD
TS:2000 USD
任何人都可以帮助我解决此问题吗?非常感谢您对我的帮助。
答案 0 :(得分:1)
这是我上一个答案的答案结合@JosefZ答案
# These variables are changed to true if the line is a header or a salary
$header = $false
$salary = $false
Get-Content .\Configuration.TXT | ForEach-Object {
# The header flag is reset each time as the header checks are made first
$header = $false
# The $salary variable is only reset back to false when a new header is reached, provided it does not contain Salary\s*Number
# Use switch to find out what type of line it is
switch -regex ($_)
{
'^Configuration' # Beginning with Configuration
{
$header = $true
}
'^;' # Beginning with semicolon
{
$header = $true
}
# Data lines begin with a space
# move to the next line - do not alter $salary variable
'^\s|^$'
{
continue
}
# If Salary Number is found set the flag
'Salary\s*Number' {
$salary = $true
}
# This is only reached once it has been determined the line is
# not a header
# not a salary header line
# not a data line
# i.e. only headers that are not Salary
# this resets the flag and makes lines eligible for output
default {
$salary = $false
}
}
# Only output lines that are not Salary and not headers
if ($salary -eq $false -and $header -eq $false) {
$_
}
} | Out-File .\Output.txt
shareeditflag