Description
Each line of the first file except the last one
  • begins with a word consisting of alphabet, digits, and underscores.
  • followed by one more more blanks,
  • then the string used to replace every occurrence of that word.
  • For example, a line func1 add instructs sed to replace every word `func1' with `add'.
  • To make sed recognize the boundary between the first and the second files, instead of appending the boundary to the end of the first file, we execute the script in the following way:
    sed -r -f scriptfile file1 - file2 <<END<ENTER>
    boundary defined<ENTER>
    END<ENTER>
    We also have to specify the boundary in Step [1] and Step [2].
  • In the following example, an empty line is used as the boundary.
Raw Input
File 1:
func1 add
func2 remove
func10 start
func20 stop
Raw Input Desired Output
File 2:
func10 ()
{ step 10-1; func1; func20;
}
func1 ()
{ step 1-1; func10; func2;
}
func20 ()
{ step 20-1; func10; step 20-2;
}
func2 ()
{ step 2-1; step 2-2;
}
start ()
{ step 10-1; add; stop;
}
add ()
{ step 1-1; start; remove;
}
stop ()
{ step 20-1; start; step 20-2;
}
remove ()
{ step 2-1; step 2-2;
}
Script and Comments
Script1
[ 1] 1,/^$/{
[ 2] /^$/!H
[ 3] d
[ 4] }
[ 5] G
[ 6] s/^([^\n]*)\n\n(.*)/\2\n\n\1/
[ 7] :loop
[ 8] s/^(\w+) +(\w+)(.*\n)(.*)\b\1\b/\1 \2\3\4\2/
[ 9] t loop
[10] s/^[^\n]*\n//
[11] /\n/b loop
Comments -r
  1. A lookup table is required to provide information about
    • what string must be replaced and
    • what string must be used as the replacement.
    It is kept in HS.
  2. Steps [1] thru [4] constitute a implicit loop which construct the lookup table according to the first datafile:
    • Step [2] appends everything except the last line to HS.
    • Step [3] deletes the current line and then makes sed start a new cycle.
    In this example, the lookup table kept in HS is:
    \nfunc1 add\nfunc2 remove\nfunc10 start\nfunc20 stop.
  3. For each line of the second datafile:
    • A new cycle reads that line to PS.
    • Steps [1] thru [4] will NOT be performed.
    • Step [5] appends the lookup table to PS.
    • Step [6] moves the lookup table to the beginning of the line. After this, the data and the last replacement of the lookup table are separated by ONE newline character. For example,
      func1 add\nfunc2 remove\nfunc10 start\nfunc20 stop\noriginal data.
    • Steps [7] thru [9] search PS for the first word and replace every occurrence of it, where
      • \w stands for the character class consisting of alphabet, digits, and underscore, and
      • \b stands for a word boundary.
    • Step [10] deletes from the lookup table in PS (not HS) the first word and the replacement.
    • If there exists non-examined word, Step [11] makes sed jump to Step [7];
    • otherwise, since the end of the script is reached, sed print the line and starts a new cycle.