Choosing a tool
cut by delimiter
cut -d: -f1 /etc/passwd
Input
alice:1000
bob:1001
Output
alice
bob
Extracts fields by delimiter. cut is the smallest choice for fixed delimiters and field numbers; see cut.
awk by whitespace
awk '{print $1}' file.txt
Input
alice 1000
bob 1001
Output
alice
bob
Extracts whitespace-separated fields. awk collapses runs of whitespace and handles conditions, arithmetic, and formatting; see awk.
awk by delimiter
awk -F: '{print $1}' /etc/passwd
Input
alice:1000
bob:1001
Output
alice
bob
Extracts fields with a custom delimiter. -F sets the separator, so awk fits once splitting needs logic; see awk.
Related links
- GNU cut manual (gnu.org)
- GNU awk manual (gnu.org)
0 Comments for this cheatsheet. Write yours!