1

I have a large log file I'm trying to extract two pieces of information from using powershell regex onto one line. The log contains all kinds of data, but the blocks I'm interested in I've given examples below. I'm trying to find the users who are logging in with the incorrect version of the app.

Sample data (It's not showing up in the preview, but there are line breaks):

Device User Name: CDALLAS
Is SSO          : false
Versions        : id=VNF-Android
0=Client-Version,VN v4.82.1;android;SM-G900V,.

Device User Name: smith5
Is SSO          : false
Versions        : id=VNF-Android
0=Client-Version,VNNEXT v4.32,.

Device User Name: joe123
Is SSO          : false
Versions        : id=VNF-iOS
0=Client-Version,APPSTORE v5.89;iPhone OS;iPhone8   1,.

What I need to return for each of these is:

CDALLAS 4.82.1

smith5 4.32

joe123 5.89

Ideally I'd want to exclude any records with 4.82.1 in it as that is the correct version.

For the user name part I can use something like (?<=Device User Name: )(.*)

How do I get the version number next to it though?
The version number is always preceded by "Client-Version," (some text) "v" (version#) then either a , or ;

Regex has always given me a headache. Any help would be greatly appreciated.

Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
Steve J
  • 11
  • 1

2 Answers2

0

This regex works should work for you:

Device User Name:\s+\b(?<name>\w+).*?(?<=Client-Version).*?v(?<version>\d[^;,]+)

Regular expression visualization

Regex Demo.

To use it in PowerShell you have to use the [System.Text.RegularExpressions.RegexOptions]::Singleline option:

$content = Get-Content 'YOUR_FILE_PATH' -raw
$regex = 'Device User Name:\s+\b(?<name>\w+).*?(?<=Client-Version).*?v(?<version>\d[^;,]+)'
[regex]::Matches($content, $regex, [System.Text.RegularExpressions.RegexOptions]::Singleline) | ForEach-Object {
    '{0} {1}' -f $_.Groups['name'].Value, $_.Groups['version'].Value
}

Output:

CDALLAS 4.82.1
smith5 4.32
joe123 5.89
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
0

looks like you already have a solution, but here is something that may be useful as well

$text = @'
Device User Name: CDALLAS
Is SSO          : false
Versions        : id=VNF-Android
0=Client-Version,VN v4.82.1;android;SM-G900V,.

Device User Name: smith5
Is SSO          : false
Versions        : id=VNF-Android
0=Client-Version,VNNEXT v4.32,.

Device User Name: joe123
Is SSO          : false
Versions        : id=VNF-iOS
0=Client-Version,APPSTORE v5.89;iPhone OS;iPhone8   1,.

'@.Split("`n")

$hash = @{}
$text | % {
    if ($_ -match ': ') {
        $split = $_.Split(':').Trim()
        $hash.Add($split[0], $split[1])
    } elseif ($_ -match ' v([0-9\.]+)') {
        $hash.Add('Version', $Matches[1])
        [pscustomobject]$hash
        $hash = @{}
    }
}

# yields:
# Is SSO Version Device User Name Versions      
# ------ ------- ---------------- --------      
# false  4.82.1  CDALLAS          id=VNF-Android
# false  4.32    smith5           id=VNF-Android
# false  5.89    joe123           id=VNF-iOS 
Anthony Stringer
  • 1,981
  • 1
  • 10
  • 15