Macos – Using sed to retrieve part of a line

macosregexsed

I have git svn command like this:

git svn log --limit=1 --oneline

It will output oneline like this:

r12345 | <anything, as it is svn comment inputted by svn-user>

I am trying to pipe in sed command so that I get the 12345 only, however, I can't get it to work.. There are a lot of errors and the current one is parenthesis not balanced

my last command was as follows:

git svn log --limit=1 --oneline | sed -e 's/r\(0-9) |*/\1/'

I've googled and the sed documentation isn't quite clear… I am not very good with regex and my best experience is with git --grep which is simplified regex with good documentation.

Environment is MacOSX terminal, if matters.

EDIT:

sed -n 's/r\(0-9*\)/\1/ works, but returns empty string.

Best Answer

Here's a regex-free solution, because

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems.

(take it with a grain of salt)

You can use cut twice:

git svn log --limit=1 --oneline | cut -d ' ' -f 1 | cut -c 2-

The first cut (cut -d ' ' -f 1) sets space as column delimiter and selects only the first column, so r12345. The second cut (cut -c 2-) selects character at position 2 and following (2-).

Related Question