Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- s/[[:space:]]*$// # remove trailing whitespace
- /^$\|^[[:space:]]*#/{p; b} # empty and sed-style comment lines: print and branch
- # printing keeps line numbers; for referencing errors
- /^\([Fs]\)\(.\)\(.*\2\)\{4\}/{ # too many delims ERROR
- s/^/# error + # /p # print a flagged/commented error
- b } # branch
- /^\([Fs]\)\(.\)\(.*\2\)\{3\}/{ # this may be a long-form 2nd delimiter
- /^\([Fs]\)\(.\)\(.*\2[[:space:]]*\2.*\2\)/{ # is long-form 2nd delimiter OK?
- s/^\([Fs]\)\(.\)\(.*\)\2[[:space:]]*\2\(.*\)\2\(.*\)/\1\2\n\3\n\4\n\5/
- t OK # branch on true to :OK
- }; s/^/# error L # /p # print a flagged/commented error
- b } # branch: long-form 2nd delimiter ERROR
- /^\([Fs]\)\(.\)\(.*\2\)\{2\}/{ # this may be short-form delimiters
- /^\([Fs]\)\(.\)\(.*\2.*\2\)/{ # is short-form delimiters OK?
- s/^\([Fs]\)\(.\)\(.*\)\2\(.*\)\2\(.*\)/\1\2\n\3\n\4\n\5/
- t OK # branch on true to :OK
- }; s/^/# error S # /p # print a flagged/commented error
- b } # branch: short-form delimiters ERROR
- { s/^/# error - # /p # print a flagged/commented error
- b } # branch: too few delimiters ERROR
- :OK # delimiters are okay
- #============================
- h # copy the pattern-space to the hold space
- # NOTE: /^s/ lines are considered to contain regex patterns, not FIXED strings.
- /^s/{ s/^s\(.\)\n/s\1/ # shrink long-form delimiter to short-form
- :s; s/^s\(.\)\([^\n]*\)\n/s\1\2\1/; t s # branch on true to :s
- p; b } # print and branch
- # The following code handles FIXED-string /^F/ lines
- s/^F.\n\([^\n]*\)\n.*/\1/ # isolate the literal find-string in the pattern-space
- s/[]\/$*.^|[]/\\&/g # convert the literal find-string into a regex of itself
- H # append \n + find-regex to the hold-space
- g # Copy the modified hold-space back into the pattern-space
- s/^F.\n[^\n]*\n\([^\n]*\)\n.*/\1/ # isolate the literal repl-string in the pattern-space
- s/[\/&]/\\&/g # convert the literal repl-string into a regex of itself
- H # append \n + repl-regex to the hold-space
- g # Copy the modified hold-space back into the pattern-space
- # Rearrange pattern-space into a / delimited command: s/find/repl/...
- s/^\(F.\)\n\([^\n]*\)\n\([^\n]*\)\n\([^\n]*\)\n\([^\n]*\)\n\([^\n]*\)$/s\/\5\/\6\/\4/
- p # Print the modified find-and-replace regular expression line
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement