一次awk的奇妙之旅(抽取gawk官方文档中所有的示例)

本文介绍了如何利用awk脚本从GNU Awk User's Guide的Texinfo源文件中抽取大量示例程序,展示了如何通过 texinfo 和 awk 技术高效地整理和实验官方文档中的编程资源。

前几天突发奇想,想看一下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函数库,

The GNU Awk User’s GuideThe GNU Awk User’s Guideicon-default.png?t=LA46https://www.gnu.org/software/gawk/manual/gawk.html#Library-Functions再细看其中的内容,有一段

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 Languageicon-default.png?t=LA46https://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/texinfoicon-default.png?t=LA46http://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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值