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

Awk is already well-known in embedded systems, eg. busybox implements a lot of POSIX using awk IMO.

Also, awk syntax is clearly the predecessor of JavaScript. The following is both awk and JavaScript:

    function f(x) {
        my_array[1] = "whatever"
        my_array["a string"] = "something else"
        if (x in my_array) return my_array[x] 
    }
It's all there: the "in" operator, no need for semicolon as statement terminator, the "function" keyword, untypedness, regular expression constants, C-like curly braces, etc.


Yeah Awk and JavaScript have surprisingly similar syntax. But the semantics are very different. Here are some things I found when studying Awk a bit more:

- Awk has no local variables! There is no "var". Believe it or not, people fake locals with unused function parameters.

- Awk has no dict literals like d={}. And no function literals like f = function(x) { return x; }

- Awk dicts can't be nested. You can't have something like: {key1: {key2: 123}} (using JS syntax)

- Awk CAN accept dicts as parameters, but it CAN'T return them. "return d" is invalid.

- Awk can't take functions as parameters OR return them. That is, no higher order functions.

The latter three facts are basically consequences of the fact that Awk has no garbage collection. It has a strict stack discipline and no nested compound data structures.


Yes, and another difference is the use of 1-based string indexes, as opposed to 0-based string indexes in JavaScript. Even though I started with C many years ago (where 0-based string indexes are there for a reason, unlike in Java and JavaScript IMHO), I found 1-based string index semantics to allow much more compact and idiomatic expressions for typical string massaging tasks.


> busybox implements a lot of POSIX using awk

Citation, please?

It looks like busybox implements the 'dos2unix', 'tac', and 'unix2dos' commands as shell scripts that call 'sed'. And there are a couple of awk scripts (e.g. 'scripts/checkhelp.awk') that are used during the busybox build process. But as far as I can tell, all of the actual busybox applets are written in plain C.

I'd love to be wrong on this point, however -- I think that implementing a full POSIX userspace with just /bin/sh and /bin/awk is an awesome idea!


Add in dollar signs before the variables and you very nearly have valid Powershell as well.

>It's all there: the "in" operator, no need for semicolon as statement terminator, the "function" keyword, untypedness, regular expression constants, C-like curly braces, etc

At some point, these polyglots must merge. It makes no sense to have arbitrary particles be the only distinction between languages.


Is there any downside to using JS instead of Awk for one-line shell scripting? Maybe it's time to retire Awk, and use js/Perl/whatever instead for that purpose?


Awk is much faster, and much more consistent in its memory management, and doesn't have some of the things that trip people up in JS (== vs ===, order of operations).

Plus, awk is probably in any *nix system, and probably doesn't need any updates to behave as expected. (And my favourite little thing: awk's executable is tiny.)




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

Search: