0

I managed to get a list of pre-commits and commits by using PowerShell script but noticed some unwanted lines on the result is there any way to filter only commit and Pre-commit-Verified from the list? user entered description data. I am new to PowerShell help is much appreciated

git log  -5 --no-merges master  | Select-String -Pattern "commit"

enter image description here

1 Answer 1

2

By default, Select-String's -Pattern parameter accepts regexes (regular expressions), which allow you to describe desired matches using sophisticated rules:

git log  -5 --no-merges master | 
  Select-String -Pattern '^commit |^\s+Precommit-'
  • ^commit matches string commit, follwed by a space, at the start of a line (^)

  • | is an alternation, meaning that the subexpression on either side is allowed to match.

  • ^\s+Precommit- matches string Precommit-, preceded by one or more (+) whitespace characters (\s) at the start of the line (^).

This regex101.com page not you to experiment with the regex interactively (the panel on the right side also contains an explanation).

Note: Select-String, like PowerShell in general, is case-insensitive by default. You can request case-sensitive matching with the -CaseSensitive switch.

Sign up to request clarification or add additional context in comments.

3 Comments

I think OP's asking how to drop commit lines not immediately followed by a Precommit-Validate line, which this doesn't answer. Can Select-String work with arbitrary line gaggles like sed can?
@jthill, honestly, I'm not sure: if you look at what's highlighted in the image in the question, one way of reading that is that only that line should be omitted. Let's see what the OP says. Select-String doesn't directly support matching across multiple lines, except if you provide the whole file as a single, multi-line string as input. But there are definitely ways to make this work.
Ah. I see it now, I was supplying context from another question. If you've got a powershell answer for OP's previous question I'd be curious too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.