0

Powershell and I think very differently. Could someone help a newbie with these 2 functions?

SpecialFilter() is supposed to be a general function to allow me to grab data from one line if it matches $dataPattern, and use it only if a line farther down in the file matches $searchPattern.

With that, FilterLog() should be able to search a git log and spit out a commit number base on the author name.

Could be git log has something built in, but I couldn't figure that out either (my day is going great!). Even if it can, let's pretend it can't. I would like to learn how to make these guys work.

function FilterLog {
    git --no-pager log -n200 | Out-File -FilePath c:\work\x.txt
    $log = get-content c:\work\x.txt
    SpecialFilter($log, "commit (.+)", "$1", "author.+Joe")
}

function SpecialFilter($list, $dataPattern, $dataReplace, $searchPattern) {  
    $results = @()  
    $data = @()

    foreach ($ln in $list) {
        if ($ln -match [string]$dataPattern) {
            $data = $ln -replace $dataReplace
            Write-Output "Data Found" # for debugging
            Write-Output $data
        } elseif ($ln -match [string]$searchPattern) {
            Write-Output "Pattern Found" # for debugging
            $results += $data
        }
    }

    Write-Output "Results:" # for debugging
    $results
}
  • The contents of the git output are not seen as a list once we get into SpecialFilter(). What am I doing wrong?
  • Surely there's a better way than using a temp file in FilterLog(). What do you suggest?
  • The "-match" isn't working. As a test, putting the "git" command directly in the SpecialFilter() ("$list = git --no-pager log -n2") gives me the output as a list like I want, but the "-match" doesn't care what I give it, it matches all lines. I suppose the variable still isn't seen as a string?
2
  • 1
    Hi Kurtis! I believe your issue is in calling SpecialFilter with parenthetical syntax, a common mistake in PowerShell (see the linked question). Commented Oct 30, 2020 at 19:28
  • Also not that you can do this without writing to file: $log = git --no-pager log -n200 Commented Oct 30, 2020 at 19:35

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.