Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

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
echo "AXIS2C_HOME=/usr/local/Axis2C" | sed 's/\(^AXIS2C_HOME=\) \(.*\)/ \2 \1/'

The output I am expecting is /usr/local/Axis2C AXIS2C_HOME=.

I can't figure out what I am doing wrong. :(

share|improve this question
Except for the space you have after _HOME=) ...which isn't present in the imput, your statement is effectively the same as Steven D's solution in his comment to jsbillings' answer... ie. your form is using extended regular expressions. (-r is required) – Peter.O Feb 21 '11 at 10:10
up vote 7 down vote accepted

The trivial answer is "more backslashes, fewer spaces":

echo "AXIS2C_HOME=/usr/local/Axis2C" | sed 's/\(^AXIS2C_HOME=\)\(.*\)/\2 \1/'

But the broader answer is, "wait, what are you trying to do?" Do you want the key-value pairs to be split into useful variables, or are you really just trying to munge the input into the reverse syntax in order to feed it to something else?

share|improve this answer
Thanks! I am just messing around :) – Ricko M Feb 8 '11 at 9:44

You have an erroneous space after the =. Try:

sed 's/\(^AXIS2C_HOME=\)\(.*\)/\2 \1/'

The following also works and is a bit shorter. \1 will be anything before the first /

sed 's|^\([^/]*\)\(/.*\)|\2 \1|'
share|improve this answer

Sed is great, but Perl can do this too, and it doesn't require the forest of backslashes:

% echo "AXIS2C_HOME=/usr/local/Axis2C" | perl -pe 's/(^AXIS2C_HOME=)(.*)/\2 \1/'
/usr/local/Axis2C AXIS2C_HOME=

Plus, you've got the full power of the regular expression engine, so you can do even more complex patterns.

share|improve this answer
You could also avoid the forest of backslashes if you have a sed such as GNU sed that supports extended regular expressions: echo "AXIS2C_HOME=/usr/local/Axis2C" | sed -r 's/(^AXIS2C_HOME=)(.*)/\2 \1/' – Steven D Feb 7 '11 at 18:32
@Steven D: The forest is my friend... (bear pun half intended... the other half is: I think I'd be lost without them :) ... but I am considering it, now that you've mentioned -r ... (I've thought about it.. I'll stick with the backslashes, otherwise I'll lose the ability to understand everyone else's sed statements :) – Peter.O Feb 21 '11 at 10:39

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.