When would you use cut instead of awk?
cut extracts fixed fields by a single-character delimiter or character positions and is simple and fast. awk handles runs of spaces, conditions and calculations. Use cut for neat CSV-like data and awk for messy whitespace-aligned output.
Explain it simply
cut is a pair of scissors that trims every line the same way. awk is a small calculator with scissors included.
cut -d: -f1 /etc/passwd takes field 1 using colon as separator. cut -c1-4 file keeps characters 1 to 4. cut -d, -f2,4 picks several fields. Output is in the original order even if you write -f4,2.
Limits of cut: the delimiter is a single character. With ls -l or ps output, columns are separated by many spaces, and cut treats each space as a separator, creating empty fields. awk treats a run of spaces as one separator, so awk '{print $2}' just works.
Choose cut for clean delimited files, such as colon- or comma-separated data. Choose awk when you need whitespace splitting, conditions, reordering of columns or arithmetic. Also remember cut cannot handle commas inside quoted CSV values; use a CSV-aware tool for that.
Example
echo "amit:1001:/home/amit" | cut -d: -f1,3
# amit:/home/amit
echo "a b c" | cut -d' ' -f2
# (empty: spaces counted individually)
echo "a b c" | awk '{print $2}'
# bWhat to say in the interview
"cut is for simple single-character delimiters or character positions. awk is better with repeated whitespace, conditions and calculations. For ps or ls output I use awk; for /etc/passwd style files, cut is enough."
Common mistakes
- Using cut -d' ' on space-aligned output and getting empty fields.