Description
  • Every double quote of the datafile is numbered except those which are escaped.
  • The job is to replace every odd-numbered double quote with a single quote,
    and every even-numbered double quote with a back quote.
Raw Input
....."test1"...."test2
test2"..not.\"...not.\"..."test3
test3"....."test4
test4
test4"...."test5".
Desired Output
.....`test1'....`test2
test2'..not.\"...not.\"...`test3
test3'.....`test4
test4
test4'....`test5'.
Script and Comments
Script1
[ 1] 1{
[ 2] x
[ 3] s/^/`/
[ 4] x
[ 5] }
[ 6] :loop
[ 7] /\n/!s/^/\n/
[ 8] s/\n(([^\\"]*|\\.)*)"/\1\n"/
[ 9] /\n"/!{
[10] s/\n//
[11] n
[12] b loop
[13] }
[14] G
[15] s/\n"(.*)\n(.)/\2\n\1/
[16] x
[17] y/`'/'`/
[18] x
[19] b loop
Comments
  1. The `-r' option must be used to make GNU sed interpret REs as EREs.
  2. The Pattern Space and the Hold Space are abbreviated to PS and HS, respectively.
  3. This script uses HS to keep the symbol, either a single or a back quote, which will be used to replace the double quote in the current substitution.
  4. Initially, we put a back quote in HS via Steps [1] thru [5].
  5. When processing a line, a newline character is used to indicate the position where a new search should begin at.
  6. Every time a line read from the datafile, a newline character is prepended at the beginning of that line by Step [6].
  7. Then Step [8] is used to move the newline character next to the double quote to be replaced in this iteration.
  8. If no such double quote found, Steps [9] thru [13] are used to remove the newline, read the next line to PS, then make sed branch to Step [6].
  9. Otherwise,
    • command `G' of Step [14] will append the symbol to be used (kept in HS) to PS.
    • Step [15] will do the substitution and move the newline to the right one character.
    • Step [16] thru Step [18] is used to `toggle' the symbol to be used in the next iteration from back to single quote or from single to back one.
    • Step [19] makes sed branch to Step [6] to see whether there exist more double quotes to be replaced.