Posts

Showing posts with the label Grep

Better Way Of Getting A GIT Commit Message By Short Hash?

Answer : git log takes (among other things): -n num to limit the number of commits shown: choose 1 (and if num is 9 or less you can just write - num , hence, -1 , for short) --pretty=format: string with directives to change the log output format. The %s directive gets the commit "subject", which is what you also get with oneline . Hence: git log -n 1 --pretty=format:%s $hash (or git log -1 --pretty=format:%s ) will do the trick here. For a complete list of format directives, see the git log documentation, under "PRETTY FORMATS" (about halfway down). Depending on how much of the commit message you actually want, there are several pretty-format specifiers that you can use: · %s: subject · %f: sanitized subject line, suitable for a filename · %b: body · %B: raw body (unwrapped subject and body) So something like git log -1 --pretty=format:%b <hash> , or use one of the other specifiers (I think %s is probably cl...

Capturing Groups From A Grep RegEx

Answer : If you're using Bash, you don't even have to use grep : files="*.jpg" regex="[0-9]+_([a-z]+)_[0-9a-z]*" for f in $files # unquoted in order to allow the glob to expand do if [[ $f =~ $regex ]] then name="${BASH_REMATCH[1]}" echo "${name}.jpg" # concatenate strings name="${name}.jpg" # same thing stored in a variable else echo "$f doesn't match" >&2 # this could get noisy if there are a lot of non-matching files fi done It's better to put the regex in a variable. Some patterns won't work if included literally. This uses =~ which is Bash's regex match operator. The results of the match are saved to an array called $BASH_REMATCH . The first capture group is stored in index 1, the second (if any) in index 2, etc. Index zero is the full match. You should be aware that without anchors, this regex (and the one using grep ) wi...