Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

224
Views
How to grab commit from grep output

Say I call the following command in a git repo:

git log --oneline -n 10 | grep pattern

and it gives me the following output:

c95383f asdfasdf pattern
3e34762 asdfasdfsd pattern

How can I grab just the commit hash from the second line so that I can pipe it into another command?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

You can consider awk for this:

git log --oneline -n 10 | awk '/pattern/ {print $1}'

Where /pattern/ matches pattern in a line while {print $1} prints first field from a matching line.

over 4 years ago · Santiago Trujillo Report

0

My friend just showed me this:

git log --oneline -n 10 | grep pattern | awk 'END{print $1}'

But I'm interested to see if anyone has any different solutions.

over 4 years ago · Santiago Trujillo Report

0

For the commits printed (ten in this case), print the hash of the oldest commit matching pattern:

git log --oneline -n 10 |
awk '$0 ~ /pattern/ {hash = $1} END {print hash}'

Same, but for the Nth newest:

git log --oneline -n 10 |
awk '$0 ~ /pattern/ && ++c==N {print $1}'

(use 1 for newest, or 2 for second newest, etc, N must be <= 10 in this example)

Print hash of Nth newest commit (no pattern):

git log --oneline -n N |
awk 'END {print $1}'

Or

git log --oneline |
awk 'NR==N {print $1}'

Remember that git log has the options --since, --after, --until, and --before, which take a date as input.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!