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

I am trying to exchange two words in a line but it doesn't work. For example: "Today is my first day of university" should be "my is Today first day of university"

This is what I tried:

sed 's/\([a-zA-z0-9]\)\([a-zA-z0-9]\)\([a-zA-z0-9]\)/\3\2\1/' filename.txt

What am I doing wrong?

share|improve this question
@warl0ck, I'm not sure we should add backslashes into sed command or fix it any other way directly in the question. – rush Mar 30 '13 at 11:05
Why do people keep changing the Q here? The OP has erroneous pattern as @rush points out in answer. This makes answer look out of place - and is no help for poster. – Runium Mar 30 '13 at 12:58
Aha. The back-slashes was there, but disappear when not as code block. Thanks @rush for Q on this. Original post plain text. – Runium Apr 1 '13 at 8:44

Try this one:

sed -r 's/([a-zA-Z0-9]+) ([a-zA-Z0-9]+) ([a-zA-Z0-9]+)/\3 \2 \1/'

Your problem is that you're tryng to use extended regex without -r option or escape symbol in sed command.

Also the regex isn't fully correct.

You're specifying incorrect range: there is no A-z range, there is A-Z.

Also you forgot spaces and you didn't specify that words are multicharacter.

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.