PowerShell - Zahl aus String extrahieren

LukS

Rear Admiral Pro
Registriert
Dez. 2009
Beiträge
5.418
Hallo,

ich stehe etwas auf der Leitung und brauche eure Hilfe.

Ich habe zum Beispiel eine der folgende Zeilen in einer Variable namens $cyaw:
Code:
Camera Yaw                      : +74.90
Camera Yaw                      : -120.3
Camera Yaw                      : -4.80
Camera Yaw                      : +160.3

Wie bekommt man in PowerShell aus einer dieser Zeilen die Zahl mit Vorzeichen und Kommastellen, so das ich nachher mit diesen Zahlen rechnen kann?

Das Funktioniert ja nicht:
Code:
$1 = $cyaw -replace '.*\s(\d+).*'

Kann mir da bitte jemand helfen?
 
Code:
@"
Camera Yaw                      : +74.90
Camera Yaw                      : -120.3
Camera Yaw                      : -4.80
Camera Yaw                      : +160.3
"@ -split "`n" | % {
	$line = $_
	if( $line -match "(?<name>.*?): (?<value>.*)" )
	{
		Write-Host -ForegroundColor Red $Matches.name.Trim()
		Write-Host -ForegroundColor Yellow ([double]$Matches.value)
	}
}
 
@JennyCB
So einfach geht das wohl nicht:
Der Wert "Camera Yaw : +74.90" kann nicht in den Typ "System.Double" konvertiert werden. Fehler: "Die Eingabezeichenfolge hat das falsche Format."
In C:\Users\LukS\Desktop\ImageMetadataEdit.ps1:36 Zeichen:9
+ $1=[double]$cyaw
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastFromStringToDoubleOrSingle

@Yuuri
Super! Danke! :daumen:
Jetzt läuft mein Script zum Editieren von JPG-Tags. :schluck:

Ich hab es so umgesetzt:
Code:
        if( $cyaw -match "(?<name>.*?): (?<value>.*)" )
        {
            #Write-Host -ForegroundColor Red $Matches.name.Trim()
            $cyaw = [double]$Matches.value
            if ($cyaw -lt 0)
            {
                $cyaw=$cyaw+360
                Write-Host ("add 360°")
            }
            Write-Host -ForegroundColor Yellow ("New CamaraYaw for GPS Img Direction: " + $cyaw)
            # in file write with exiftool the CameraYaw into the GPSImgDirection tag
            & $etpath $files[$i].FullName "-GPSImgDirection=$cyaw"

            #TEST: read the GPSImgDirection tag
            Write-Host -ForegroundColor Magenta ("In file " + $files[$i].Name + " saved GPSImgDirection: ")
            & $etpath $files[$i].FullName "-GPSImgDirection"
        }
 
Zuletzt bearbeitet:
Es gibt auch noch die Konvertierungsfunktionen in [Convert]:

Code:
PS PS:\> [convert]::ToDouble('+18.1')
18.1
PS PS:\>
 
Zurück
Oben