Skip to content

Instantly share code, notes, and snippets.

@dkuppitz
Last active September 19, 2017 17:16
Show Gist options
  • Select an option

  • Save dkuppitz/023dc5cc8f392ef2b9663915cf5b1bd9 to your computer and use it in GitHub Desktop.

Select an option

Save dkuppitz/023dc5cc8f392ef2b9663915cf5b1bd9 to your computer and use it in GitHub Desktop.
cat input.asciidoc | awk -f replace.awk > output.asciidoc

Header

Subheader

SubSubHeader

blabla

SubSubSubHeader

Next SubSubSub has an error (on purpose).

SubSubSubHeader2

Subway

Done.

Header

Subheader

SubSubHeader

blabla

SubSubSubHeader

Next SubSubSub has an error (on purpose).

SubSubSubHeader2

Subway

Done.

BEGIN {
last = -1
chars[1] = "="
chars[2] = "-"
chars[3] = "~"
chars[4] = "^"
chars[5] = "+"
}
{ if (last != -1) {
l = length($0)
if (l > 0 && l == length(last)) {
pl = 1
for (i in chars) {
h = sprintf("%" l "s", "") ; gsub(/ /, chars[i], h)
if ($0 == h) {
prefix = sprintf("%" i "s", "") ; gsub(/ /, "=", prefix)
last = prefix " " last
pl = 0
break
}
}
if (pl) print last
} else {
print last
last = $0
}
} else last = $0
}
END {
print last
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment