Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

TXR Lisp Awk macro: http://www.nongnu.org/txr/txr-manpage.html#N-000264BC

Has analogs for all salient POSIX Awk features and most GNU Awk extensions. (Of course, not semantic cruft like the weak type system, or uninitialized variables serving as zero in arithmetic.)

Plus:

* You can embed (awk ...) expressions anywhere, including other (awk ...) expressions.

* You can capture a delimited continuation (awk ...) and yield out of there.

* It supports richer range expressions than Awk. Range expressions combine with other range expressions unlike in Awk, so that you can express a range which spans from one range to another. Also, there are variations of the operator to exclude either endpoint of the range: rng, -rng, rng- and -rng-.

* You can "awk" over a list of strings, possibly an infinitely lazy one.

    1> (awk (:inputs '("a" "b") '("c" "d"))
            (t (prn nr fnr rec)))
    1 1 a
    2 2 b
    3 1 c
    4 2 d
    nil
* It has a return value: whatever the last :end returns, or else nil:

    1> (awk (:end 42) (:end 43))
    [Ctrl-D]
    43
Build a list from the first fields of /etc/passwd:

  1> (build
       (awk (:inputs "/etc/passwd")
            (:set fs ":")
            (t (add [f 0]))))
  ("root" "daemon" "bin" "sys" "sync" "games" "man" "lp" "mail"
   "news" "uucp" "proxy" "www-data" "backup" "list" "irc" "gnats"
   "nobody" "libuuid" "syslog" "messagebus" "avahi-autoipd" "avahi"
   "usbmux" "gdm" "speech-dispatcher" "kernoops" "pulse" "rtkit"
   "hplip" "saned" "kaz" "vboxadd" "sshd" "oprofile" "ntp" "lightdm"
   "colord~" "whoopsie" "postfix")

Type conversion of fields (which are just strings) is achieved by an elegant operator fconv which takes a condensed notation such as (fconv i : r : xz) which means convert the first field to integer as a decimal integer, the last field as a hexadecimal integer and the fields in between as reals. The xz means that if the last field is invalid, it gets converted to zero rather than nil. These letters are just the names of lexical functions available in the awk scope, rather than built-in fconv behaviors.



Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: