onetrueawk / awk

One true awk
Other
2.01k stars 160 forks source link

awk only gets first record when RS="\0" #119

Closed phillbush closed 3 years ago

phillbush commented 3 years ago

The following programs, which list the files in the current directory, work on GNU awk, but on One True Awk™ they only list the first found file (i.e., the first record). (Run them on a directory with more than one files)

#!/usr/bin/awk -f

function ls(dir,    i, n, s, a, cmd) {
    cmd = "find \"" dir "\" -mindepth 1 -maxdepth 1 -print0"
    while ((cmd | getline s) > 0) {
        a[++n] = s
    }
    close(cmd)
    printf "%d file%s\n", n, ((n > 1) ? "s" : "")
    for (i = 1; i <= n; i++) {
        printf "%s\n", a[i]
    }
}

BEGIN {
    RS="\0"
    ls(".")
}
find . -mindepth 1 -maxdepth 1 -print0 | awk 'BEGIN{RS="\0"} 1'
arnoldrobbins commented 3 years ago

This awk uses C strings internally. RS="\0" is the same as saying RS="". That's not going to change. If you need NUL as a separator, you have to use gawk.