Lessons · Terminal · runs of spaces are not fields
When the columns are spaces
cut treats every delimiter as a boundary, so two spaces in a row mean an empty field between them.
Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.
What it is for
Command output is nearly always space-aligned, which is exactly the shape cut is bad at.
How to think about it
One consistent character: cut. Runs of whitespace: awk.
Worked example
cut -d' ' -f2 spaced.txtThe second line gives 4; the first gives nothing, because field 2 is an empty gap.
awk '{print $2}' spaced.txtawk splits on runs of whitespace, so $2 is the second COLUMN in both lines.Your turn
Print the second whitespace-separated column of app.log.
'{print $2}' app.logTry it at a real prompt
The trap
This is the one job cut genuinely cannot do. Reaching for a cleverer -d will not fix it.
Practise runs of spaces are not fields on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.