In order to update these numeric END-POINT values, I'd suggest you use switch
with option -Regex
and loop through the file line-by-line:
$endPointRegex = '(?<endpoint>s*END-POINTs{4})(?<num1>d+(?:.d+))?s+(?<num2>d+(?:.d+))?s+(?<num3>d+(?:.d+))?(?<rest>.*)'
$result = switch -Regex -File 'D:TestMyFile.txt' {
$endPointRegex {
'{0}{1} {2} {3}{4}' -f $matches['endpoint'],
([double]$matches['num1'] / 12.0),
([double]$matches['num2'] / 12.0),
([double]$matches['num3'] / 12.0),
$matches['rest']
}
default { $_ }
}
# output on screen
$result
# output to new file
$result | Set-Content -Path 'D:TestMyNewFile.txt' -Force
Output:
COMPONENT-IDENTIFIER 1
ATTRIBUTE9 C
ATTRIBUTE22 0
END-POINT 43225.0416666667 46308.375 2769.56666666667 1 SL
END-POINT 43208.375 46308.375 2769.56666666667 1 SL
WEIGHT 2.177
UBV {111256-254885-000-1515-BGL518FS7D}
END-POINT 43225.0416666667 46308.375 2769.56666666667 1 PL
END-POINT 43208.375 46308.375 2769.56666666667 1 PL
ATTRIBUTE15 D
ATTRIBUTE08 3
If you want the numbers devided by 12.0 also have 3 decimals like the original values, change to:
$endPointRegex = '(?<endpoint>s*END-POINTs{4})(?<num1>d+(?:.d+))?s+(?<num2>d+(?:.d+))?s+(?<num3>d+(?:.d+))?(?<rest>.*)'
$result = switch -Regex -File 'D:TestMyFile.txt' {
$endPointRegex {
'{0}{1:F3} {2:F3} {3:F3}{4}' -f $matches['endpoint'],
([double]$matches['num1'] / 12.0),
([double]$matches['num2'] / 12.0),
([double]$matches['num3'] / 12.0),
$matches['rest']
}
default { $_ }
}
# output on screen
$result
# output to new file
$result | Set-Content -Path 'D:TestMyNewFile.txt' -Force
Output:
COMPONENT-IDENTIFIER 1
ATTRIBUTE9 C
ATTRIBUTE22 0
END-POINT 43225.042 46308.375 2769.567 1 SL
END-POINT 43208.375 46308.375 2769.567 1 SL
WEIGHT 2.177
UBV {111256-254885-000-1515-BGL518FS7D}
END-POINT 43225.042 46308.375 2769.567 1 PL
END-POINT 43208.375 46308.375 2769.567 1 PL
ATTRIBUTE15 D
ATTRIBUTE08 3
Regex details:
(?<endpoint> Match the regular expression below and capture its match into backreference with name “endpoint”
s Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
* Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
END-POINT Match the characters “END-POINT” literally
s Match a single character that is a “whitespace character” (spaces, tabs, line breaks, etc.)
{4} Exactly 4 times
)
(?<num1> Match the regular expression below and capture its match into backreference with name “num1”
d Match a single digit 0..9
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
(?: Match the regular expression below
. Match the character “.” literally
d Match a single digit 0..9
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
)? Between zero and one 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.)
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
(?<num2> Match the regular expression below and capture its match into backreference with name “num2”
d Match a single digit 0..9
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
(?: Match the regular expression below
. Match the character “.” literally
d Match a single digit 0..9
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
)? Between zero and one 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.)
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
(?<num3> Match the regular expression below and capture its match into backreference with name “num3”
d Match a single digit 0..9
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
(?: Match the regular expression below
. Match the character “.” literally
d Match a single digit 0..9
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
)? Between zero and one times, as many times as possible, giving back as needed (greedy)
(?<rest> Match the regular expression below and capture its match into backreference with name “rest”
. 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)
)
Mind you, these numbers will format with decimal comma's if you are in a country that uses the decimal comma instead of a decimal point. Since you're in Houston, you won't have to deal with that