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

Maybe awk for text manipulation?


I always thought that the original idea of Perl was slapping sh, sed and awk together into one language.


Literally the reason AWK was created


Having extensively used AWK and PERL, I don’t agree. AWK works for simple things, but has significant limitations especially for more complex parsing you might do in a dozen lines of PERL. Subroutines don’t return complex structures, Strict mode declaration checking, hashes, foreach style iterators are much less functional or missing in AWK. Multi layer structures are also trivial in PERL (hash table or array composed of child hash or array). Finally, AWK versus GAWK versus NAWK compatibility is an issue.


AWK has been and will forever been under appreciated. It is a fantastic tool. I always preferred it to Perl myself.


I use it multiple times a week for printing/copying tabular data from space-delimited files. It's not the most arcane usage but it would probably take several lines to do what awk does in one. I don't even know much awk! Learning any bit of awk is worth every moment of the investment.

    cat somefile.txt | awk '{ print $1 "\t" $2 }'
On mac I pipe to `pbcopy`. Saves me several minutes each time.


Bash is actually pretty decent at this by itself if you don't have a lot of extra columns:

    cat somefile.txt | while IFS=" " read one two; do echo -e "$one\t$two"; done
IME the pattern matching is where awk really shines.


Also see basic Unix utilities like cut / paste (and colrm if your *nix has it, not sure if MacOS does) as these do the same column selection and printing and support simple options for delimiter and field number selection instead of writing a parser.

cut -d’ ‘ -f 1,2 < some file.txt


I do have a ton of extra columns I'm filtering out but that's really good. I'll try it on. Thanks!


I don't use it quite that frequently but awk/sed is a strong addition to the toolbox.




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

Search: