前几天突发奇想,想看一下awk中所有的内置函数,于是google找到了The GNU Awk User’s GuideThe GNU Awk User’s Guidehttps://www.gnu.org/software/gawk/manual/gawk.htmlThe GNU Awk User’s Guide,浏览目录的时候,居然发现有一节是讲一个awk函数库,
11.3.7 Extracting Programs from Texinfo Source Files
Both this chapter and the previous chapter (A Library of awk Functions) present a large number of
awk
programs. If you want to experiment with these programs, it is tedious to type them in by hand. Here we present a program that can extract parts of a Texinfo input file into separate files.This Web page is written in Texinfo, the GNU Project’s document formatting language. A single Texinfo source file can be used to produce both printed documentation, with TeX, and online documentation. (Texinfo is fully documented in the book Texinfo—The GNU Documentation Format, available from the Free Software Foundation, and also available online.)
意思就是可以从texinfo文件中抽取所有的示例代码,这相当给力啊,那就行动起来。首先了解下什么是texinfo
使用Texinfo创建的可打印的文档包括一本书的所有普通特征,如章、节、交叉引用和索引。
通过一份Texinfo源文件,可以创建一份Info文件,一份HTML文档,一份XML文件或者一份pdf文件。Texinfo源文件是普通的ASCII文件,它包含交互的@命令文本。
接下来是我的提取过程:
1.首先找到gawk官方文档的texinfo文件
Recipe for a Programming Languagehttps://fossies.org/linux/misc/gawk-5.1.1.tar.xz/gawk-5.1.1/doc/gawk.texi?m=b下完之后就拿到了gawk.texi
2.安装texinfo
1.下载texinfo-6.8.tar.gz Index of /gnu/texinfohttp://ftp.gnu.org/gnu/texinfo/
2.解压
#tar zxvf texinfo-6.8.tar.gz
3.配置生成Makefile
#./configure --prefix=/usr
4.编译
#make
5.安装
#make install
3.为了查看方便,我通过textinfo生成了一个本地的站点,
makeinfo --html gawk.texi
4.准备抽取代码的awk脚本文件 extract.awk
BEGIN { IGNORECASE = 1 }
/^@c(omment)?[ \t]+system/ \
{
if (NF < 3) {
e = (FILENAME ":" FNR)
e = (e ": badly formed `system' line")
print e > "/dev/stderr"
next
}
$1 = ""
$2 = ""
stat = system($0)
if (stat != 0) {
e = (FILENAME ":" FNR)
e = (e ": warning: system returned " stat)
print e > "/dev/stderr"
}
}
/^@c(omment)?[ \t]+file/ \
{
if (NF != 3) {
e = (FILENAME ":" FNR ": badly formed `file' line")
print e > "/dev/stderr"
next
}
if ($3 != curfile) {
if (curfile != "")
close(curfile)
curfile = $3
}
for (;;) {
if ((getline line) <= 0)
unexpected_eof()
if (line ~ /^@c(omment)?[ \t]+endfile/)
break
else if (line ~ /^@(end[ \t]+)?group/)
continue
else if (line ~ /^@c(omment+)?[ \t]+/)
continue
if (index(line, "@") == 0) {
print line > curfile
continue
}
n = split(line, a, "@")
# if a[1] == "", means leading @,
# don't add one back in.
for (i = 2; i <= n; i++) {
if (a[i] == "") { # was an @@
a[i] = "@"
if (a[i+1] == "")
i++
}
}
print join(a, 1, n, SUBSEP) > curfile
}
}
function join(array, start, end, sep, result, i)
{
if (sep == "")
sep = " "
else if (sep == SUBSEP) # magic value
sep = ""
result = array[start]
for (i = start + 1; i <= end; i++)
result = result sep array[i]
return result
}
function unexpected_eof() {
printf("%s:%d: unexpected EOF or error\n",
FILENAME, FNR) > "/dev/stderr"
exit 1
}
END {
if (curfile)
close(curfile)
}
5.抽取动作
awk -f extract.awk gawk.texi
执行完之后,就会在当前目录生成一个eg的文件夹,
root@VirtualBox:/home/root/tools/gawk/eg# ls
data lib misc prog