# Moving parts of a string with a Regex in sed

I am trying to replace,

input.output.roomMAX[0]        to   input.output.room[0].MAX
input.output.roomMAX[1]        to   input.output.room[1].MAX
input.output.roomMIN[variable] to   input.output.room[variables].MIN


I am getting stuck on how to match things and reference them in the replacement. What argument do I give to sed?

-

## 1 Answer

sed 's/room$$MAX\|MIN$$$$$[^]]*$$$/room\2.\1/' inputfile


If you want to change "variable" to "variables" add another instruction:

sed 's/room$$MAX\|MIN$$$$$[^]]*$$$/room\2.\1/;s/variable/variables/' inputfile

-
Or: sed -r 's/room(MIN|MAX)($.*$)/room\2.\1/' with less escaping. –  slhck Jul 27 '12 at 7:41
@slhck: If your version of sed supports it (or -E for OS X). –  Dennis Williamson Jul 27 '12 at 7:43
Yeah. At some point I got used to not using BSD sed anymore and using gsed, on OS X at least. –  slhck Jul 27 '12 at 7:44