#! /usr/bin/gawk -f # Last edited on 2019-04-23 19:07:54 by stolfilocal # Replaces any named online reference by a remote link , # and writes the extracted ref to a file ".therefs". BEGIN { ofile = ".therefs" } // { lin = $0 new="" while (match(lin, /\/]*>/) > 0) { # Skip part before the new = (new substr(lin, 1, RSTART-1)) lin = substr(lin, RSTART) # Extract the tag : tag = (substr(lin,1,RLENGTH-1) "/>") # Find the closing : if (match(lin, /<[ ]*[\/][ ]*ref[ ]*>/) == 0) { printf "line %d: ** ref not closed [%s]\n", FNR, lin > "/dev/stderr" break } else { #Extract the text of the reference: ref = substr(lin, 1, RSTART-1+RLENGTH) lin = substr(lin, RSTART+RLENGTH) # Write the to the output file: print ref > ofile print "" > ofile # Copy the to the new output line, with final "/": new = (new tag) } } new = (new lin); lin = ""; print new next } END { close(ofile) }