从PowerShell text/json输出文件中获取特定字段的所有值



我在Powershell中查询了一个巨大的文本文件输出。它的一部分是这样的。现在,我感兴趣的是打印/只选择特定字段的数值。costInBillingCurrency。我只是想从整个大文本文件中删除"costInBillingCurrency":0.003038455451812。


"costInBillingCurrency":0.003038455451812,"costInPricingCurrency":0.0031903782244026,"costCenter":"","date":"2022-12- 00:00:00Z","exchangeRate":"0.9617696561673479201731185381101226","exchangeRateDate":"2022-12-01T00:00:00Z"

我希望输出类似于这样的东西,以便我可以将其输入到CSV文件中。我在形成正则表达式和找到正确的命令在Powershell终端中使用来形成我的脚本时遇到了麻烦。

tbody> <<tr>
header 1Values
costInBillingCurrency0.003038455451812
costInBillingCurrency6.003038455451812

根据我的评论,这并不复杂。

一种方法是:

Clear-Host
'"costInBillingCurrency":0.003038455451812,"costInPricingCurrency":0.0031903782244026,"costCenter":"","date":"2022-12- 00:00:00Z","exchangeRate":"0.9617696561673479201731185381101226","exchangeRateDate":"2022-12-01T00:00:00Z"' -replace ',.*' | 
ConvertFrom-Csv -Delimiter ':' -Header header1, Values
# Results
<#
header1               Values           
-------               ------           
costInBillingCurrency 0.003038455451812
#>

或者这样:

Clear-Host
('"costInBillingCurrency":0.003038455451812,"costInPricingCurrency":0.0031903782244026,"costCenter":"","date":"2022-12- 00:00:00Z","exchangeRate":"0.9617696561673479201731185381101226","exchangeRateDate":"2022-12-01T00:00:00Z"' -split ',', 2)[0] | 
ConvertFrom-Csv -Delimiter ':' -Header header1, Values
# Results
<#
header1               Values           
-------               ------           
costInBillingCurrency 0.003038455451812
#>

或者如果你真的需要RegEx, this.

Clear-Host 
([RegEx]::Matches('"costInBillingCurrency":0.003038455451812,"costInPricingCurrency":0.0031903782244026,"costCenter":"","date":"2022-12- 00:00:00Z","exchangeRate":"0.9617696561673479201731185381101226","exchangeRateDate":"2022-12-01T00:00:00Z"', '(?m)^[^rn,]+')).Value| 
ConvertFrom-Csv -Delimiter ':' -Header header1, Values
# Results
<#
header1               Values           
-------               ------           
costInBillingCurrency 0.003038455451812
#>

最新更新