Search code examples
powershellsplitbreakopenweathermap

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


:My Script saves with Out-File some Sentences in a File. But when it saves it its not ordered so i saved the output in a variable and joined the spaces.

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

The Variable contains the function from an API of openweathermap and looks like this:

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

If i dont join it, it looks like this in the file:

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

Now its all in one line like this:

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)

But I want a break after every forecast so:

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)

I already tried with -split but there was this error:
parsing ")" - Too many )'s.

I just tried it to split with "Oct" like this:

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

And the Output in the File looks like this:

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)

Does anyone know why the Oct dissappears and how i can get it back? I would be happy if anybody would help me. Thanks


Solution

  • That looks like a very weird format indeed..

    I would go through that file one line at a time and build the header line
    (Forecast for zurich next 3 days:) and the daily forecast lines like below:

    $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
    

    Output:

    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)