My question is inspired by an interesting question somebody asked at http://tex.stackexchange.com and my attempt to provide the AWK solution. Note AWK here means NAWK since as we know gawk != awk
. I am reproducing a bit of that answer here.
Original question:
I have a rather large document with lots of math notation. I've used |foo|
throughout to indicate the absolute value of foo. I'd like to replace every instance of |foo|
with \abs{foo}
, so that I can control the notation via an abs macro I define.
My answer:
This post is inspired by cmhughes proposed solutions. His post is one of the most interesting posts on TeX editing which I have ever read. I just spent 2 hours trying to produce nawk solution. During that process I learned that AWK not only doesn't support non-greedy regular expressions which is to be expected since it is sed's cousin but even worse AWK regular expression does not capture its groups. A simple AWK script
#!/usr/bin/awk -f
NR>0{
gsub(/\|([^|]*)\|/,"\\abs{\1}")
print
}
Applied to the file
$|abs|$ so on and so fourth
$$|a|+|b|\geq|a+b|$$
who is affraid of wolf $|abs|$
will unfortunately produce
$\abs{}$ so on and so fourth
$$\abs{}+\abs{}\geq\abs{}$$
who is affraid of wolf $\abs{}$
An obvious fix for above solution is to use gawk instead as in
awk '{print gensub(/\|([^|]*)\|/, "\\abs{\\1}", "g", $0)}'
However I wonder if there is a way to use an external regex library from AWK for example tre. Even more generally how does one interface AWK with the C code (the pointer to documentation would be OK).