Advertisement
dropbox1349

table-to-regex.sed

Jul 12th, 2012
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.69 KB | None | 0 0
  1. s/[[:space:]]*$// # remove trailing whitespace
  2.  
  3. /^$\|^[[:space:]]*#/{p; b} # empty and sed-style comment lines: print and branch
  4. # printing keeps line numbers; for referencing errors
  5.  
  6. /^\([Fs]\)\(.\)\(.*\2\)\{4\}/{ # too many delims ERROR
  7. s/^/# error + # /p # print a flagged/commented error
  8. b } # branch
  9.  
  10. /^\([Fs]\)\(.\)\(.*\2\)\{3\}/{ # this may be a long-form 2nd delimiter
  11. /^\([Fs]\)\(.\)\(.*\2[[:space:]]*\2.*\2\)/{ # is long-form 2nd delimiter OK?
  12. s/^\([Fs]\)\(.\)\(.*\)\2[[:space:]]*\2\(.*\)\2\(.*\)/\1\2\n\3\n\4\n\5/
  13. t OK # branch on true to :OK
  14. }; s/^/# error L # /p # print a flagged/commented error
  15. b } # branch: long-form 2nd delimiter ERROR
  16.  
  17. /^\([Fs]\)\(.\)\(.*\2\)\{2\}/{ # this may be short-form delimiters
  18. /^\([Fs]\)\(.\)\(.*\2.*\2\)/{ # is short-form delimiters OK?
  19. s/^\([Fs]\)\(.\)\(.*\)\2\(.*\)\2\(.*\)/\1\2\n\3\n\4\n\5/
  20. t OK # branch on true to :OK
  21. }; s/^/# error S # /p # print a flagged/commented error
  22. b } # branch: short-form delimiters ERROR
  23.  
  24. { s/^/# error - # /p # print a flagged/commented error
  25. b } # branch: too few delimiters ERROR
  26.  
  27. :OK # delimiters are okay
  28. #============================
  29. h # copy the pattern-space to the hold space
  30.  
  31. # NOTE: /^s/ lines are considered to contain regex patterns, not FIXED strings.
  32. /^s/{ s/^s\(.\)\n/s\1/ # shrink long-form delimiter to short-form
  33. :s; s/^s\(.\)\([^\n]*\)\n/s\1\2\1/; t s # branch on true to :s
  34. p; b } # print and branch
  35.  
  36. # The following code handles FIXED-string /^F/ lines
  37.  
  38. s/^F.\n\([^\n]*\)\n.*/\1/ # isolate the literal find-string in the pattern-space
  39. s/[]\/$*.^|[]/\\&/g # convert the literal find-string into a regex of itself
  40. H # append \n + find-regex to the hold-space
  41.  
  42. g # Copy the modified hold-space back into the pattern-space
  43.  
  44. s/^F.\n[^\n]*\n\([^\n]*\)\n.*/\1/ # isolate the literal repl-string in the pattern-space
  45. s/[\/&]/\\&/g # convert the literal repl-string into a regex of itself
  46. H # append \n + repl-regex to the hold-space
  47.  
  48. g # Copy the modified hold-space back into the pattern-space
  49.  
  50. # Rearrange pattern-space into a / delimited command: s/find/repl/...
  51. s/^\(F.\)\n\([^\n]*\)\n\([^\n]*\)\n\([^\n]*\)\n\([^\n]*\)\n\([^\n]*\)$/s\/\5\/\6\/\4/
  52.  
  53. p # Print the modified find-and-replace regular expression line
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement