每次出现某个字符后,如何中断一段?

How can I brake a paragraph after every time a certain character shows up?

:我的脚本使用 Out-File 将一些句子保存在一个文件中。但是当它保存它时它没有排序所以我将输出保存在一个变量中并加入空格。

$weatherInfo -join ""| Out-File C:\Wetterbot\Wetter$Datum.txt

变量包含来自 openweathermap API 的函数,如下所示:

$forecastInfo = Write-WeatherForecast -City $place -ApiKey $ApiKey -Units metric -Days $tage 6>&1

如果我不加入它,它在文件中看起来像这样:

Forecast for 
zurich
 next 1 day:
Oct 22
: 
9.285°C
 (☁️ broken clouds)
Oct 23
: 
7.64°C
 (☁️ broken clouds)

现在它像这样在一行中:

Forecast for zurich next 3 days:Oct 22: 9.285°C (☁️ broken clouds)Oct 23: 7.64°C (☁️ broken clouds)Oct 24: 7.94°C (☀️ clear sky)Oct 25: 10.99°C (☁️ few clouds)

但我想在每次预测后休息一下,所以:

Forecast for zurich next 3 days:
Oct 22: 9.285°C (☁️ broken clouds)
Oct 23: 7.64°C (☁️ broken clouds)
Oct 24: 7.94°C (☀️ clear sky)
Oct 25: 10.99°C (☁️ few clouds)

我已经尝试过 -split 但出现了这个错误:
正在解析 ")" - ) 太多。

我只是试过像这样用“Oct”拆分:

$forecastInfo -join "" -split "Oct"|Out-File C:\Wetterbot\Vorhersage$Datum.txt

文件中的输出如下所示:

Forecast for zurich next 3 days:
 22: 9.155°C (☁️ broken clouds)
 23: 7.64°C (☁️ broken clouds)
 24: 7.94°C (☀️ clear sky)
 25: 10.99°C (☁️ few clouds)

有谁知道为什么 Oct 消失了,我怎样才能找回它?如果有人能帮助我,我会很高兴。谢谢

这看起来确实是一种非常奇怪的格式..

我会一次一行地浏览该文件并构建 header 行
(Forecast for zurich next 3 days:) 和如下的每日预测线:

$Datum    = Get-Date -Format 'yyyyMMdd HH.mm.ss'  # just guessing here..
$header   = @()
$forecast = @()
$inHeader = $true
# use switch to read and parse the file line-by-line
$result = switch -Regex -File "C:\Wetterbot\Wetter$Datum.txt" {
    '^[a-z]{3}\s\d+'  { 
        # starting a new daily forecast
        if ($inHeader) { 
            $inHeader = $false
            # output the header line
            $header -join ' '
        }
        if ($forecast.Count) {
            # output the previous day forecast line
            $forecast  -join ' ' -replace ' :', ':'
            $forecast = @()  # clear it for the next lines
        }
        $forecast += $_.Trim()
    }
    # I know you shoud avoid concatenating with `+=`, but since 
    # this concerns only a few items I guess it won't bother that much.
    default { if ($inHeader) { $header += $_.Trim() } else {$forecast += $_.Trim() } }
}
# finish up with the final forecast
if ($forecast.Count) { $result += $forecast -join ' ' -replace ' :', ':'}

# output on screen
$result

# output to (new) text file
$result | Set-Content -Path "C:\Wetterbot\Wetter$Datum.txt" -Encoding utf8

输出:

Forecast for zurich next 3 days:
Oct 22: 9.285°C (☁️ broken clouds)
Oct 23: 7.64°C (☁️ broken clouds)
Oct 24: 7.94°C (☀️ clear sky)
Oct 25: 10.99°C (☁️ few clouds)