Description
Give a string of the form 'path {....}' where the text inside the braces is a sequence of one-byte codes used to describe the motion of a robot. The following table lists valid available one-byte codes and their corresponding actions:
anorth
bwest
csouth
deast
eup
fdown
Each one-byte code may be preceded by one integer, which means that action must be performed that number of times.
Raw Input
path {baecdf}
path {2a3be4c2d3f}
Desired Output
path {west;north;up;south;east;down;}
path {2north;3west;up;4south;2east;3down;}
Script and Comments
Script1
[ 1] 1{
[ 2] x
[ 3] s/^/a=north;b=west;c=south;d=east;e=up;f=down/
[ 4] x
[ 5] }
[ 6] s/\{/{\n/
[ 7] G
[ 8] :loop
[ 9] s/\n([0-9]*)(.)(.*)\2=([^;]*)/\1\4;\n\3\2=\4/
[10] t loop
[11] s/\n//
[12] s/\n.*//
Comments
  1. Import Notes
    You have to use the '-r' option of GNU sed to run this script, then GNU sed will interpret REs as Extended REs, where we do not need to escape grouping parentheses, and braces used for repetition operators, but HAVE to escape literal ones.
  2. Before processing any input data, Step [1] thru Step [5] will save the mapping table in Hold Space, in the form code = action ; code = action ....
  3. Step [6] will insert a newline character before the first byte-code, which will be removed by Step [11].
  4. Step [7] is used to append the mapping table to the current line, and separate them with a newline character.
  5. In Step [9],
    • the second pair of parentheses is used to group the RE ., which matches the byte code.
    • Later, we use that byte-code (referenced by \2) to find its corresponding action(matches [^;]*).
  6. Step [12] will remove the mapping table from current line.