Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to get everything on the matched line excluding the match using grep.

If I have

#define VERSION 0.1

The command should echo


I saw this question, but I only want things on the same line.

I read the man page, but I don't see anything that matches my specific usage case. Would a different command possibly be better than grep for this?

share|improve this question
So you're searching for #define VERSION? – Dennis Mar 6 '13 at 14:45
Yeah. What I'm specifically doing is executing a shell command to get what VERSION is defined as and storing it in a macro in a Makefile. – Zach Latta Mar 6 '13 at 14:46
up vote 2 down vote accepted

An easy way to achieve this is piping grep's output to sed:

command | grep "^#define VERSION" | sed 's/^#define VERSION //'

You can achieve the same result using only sed if you use the -n switch and the p (i.e., print) pattern for the regular expression. This will replace and only print lines that have been modified:

command | sed -n 's/^#define VERSION //p'

See: man sed

share|improve this answer
Thanks! (extra characters) – Zach Latta Mar 6 '13 at 14:50

If your version of grep supports perl regex you can do it like this:

grep -oP '(?<=#define VERSION )[^ ]*$'

Otherwise use two invocations of grep:

grep '#define VERSION' | grep -o '[^ ]*$'
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.