3 ms·
Python's just a different tool. Sed and awk fall down with a sufficiently involved task, but they shine in duct taping something together quickly at the command
by billjings 15y ago
Python's just a different tool. Sed and awk fall down with a sufficiently involved task, but they shine in duct taping something together quickly at the command line or in a short shell script.
Two reasons why: one, everything you realistically would want to do with sed and awk is available immediately. You don't have to import any libraries or do any setup work, and the language itself implicitly assumes that you're iterating over delimited text. Getting to '/^GET/ { print $3 }' takes a lot more characters in python than awk.
Two is that in the case of awk, the language model is superior for simple parsing of structured text. If I want to extract some content from a specific XML file, for example, the event based programming model gets me there quickly and without any specialized libraries.
Of course, python wins as your problems get harder and veer away from sed+awk's strengths. My rule of thumb is that as soon as I start thinking I should break things out into functions, I switch to a stouter programming language.
- cema 15y agoGetting to '/^GET/ { print $3 }' takes a lot more characters in python than awk. True. Then there is Perl which is closer to awk/sed (intentionally, too) and thus more compact than Python. perl -lane 'print $F[2] if (-m /^GET/)' It tends to grow unwieldy as the problems (more precisely, solutions) become more complex.