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

Among my favorites is this little zinger (awk):

    !seen[$0]++
which gives the union of the lines of all the input files, with none of that 'sort | uniq' business.

It's not a one-liner, but here's intersection:

    ! buf[$0]++ { acc[$0]++ }

    ENDFILE {
        delete buf;
        files++
    }

    END {
        for (k in acc) if (acc[k] == files) print k
    }
and here's set difference:

    ! filenum { acc[$0] = 1    }
    filenum   { delete acc[$0] }

    ENDFILE { filenum++ }

    END {
        for (k in acc) print k
    }
I keep these in my ~/bin with evocative names (e.g., 'union' and 'set-diff'). Once I did that, lots of other things became one liners.


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

Search: