3 ms·
Why not `… | cut -f1 -d ” ”` though?
by cluckindan 1y ago
Why not `… | cut -f1 -d ” ”` though?
- stevekemp 1y agoSpeaking personally I find that I always use awk for printing (numbered) fields in shell-scripts, and when typing quick ad-hoc commands at the prompt. The main reason for this is that it's easy to add a grep-like step there, without invoking another tool. So I can add a range-test, a literal-test, or something more complex than I could do with grep. In short I can go from: awk '{ print $2 }' To this, easily: awk '$1 == "en4:" {print $NF}'
- cluckindan 1y agoYet using cut, even with extra piping to tr or grep before, is probably over two times faster than awk, which may or may not matter depending on what you’re doing.
- efrecon 1y agoBecause awk understands "columns" better. echo '999 888' | awk '{print $2}' prints 888
- cluckindan 1y agoHow is that different from echo '999 888' | cut -f2 -d " " except for the default delimiter being space for awk?
- Eduard 1y agoecho '999 888' | cut -f2 -d " " # notice two consecutive spaces returns NULL.
- cluckindan 1y agoRight, that would be expected though? I suppose awk is better for parsing formatted (padded) output.
- xorcist 1y agoThere are invalid column separators for awk too. To handle consecutive spaces in cut you have squeeze them: echo '999 888' | tr -s " " | cut -f2 -d " "
- bregma 1y agoHow can it be "better" when it is exactly the same?