accounts.txt uses colons as separators and its first field is a user name.
Print only the user-name field.
Which command is appropriate?
cut -d DELIMITER -f LIST splits records using the delimiter and extracts the requested field numbers.
Detailed explanation
cut -c1 accounts.txtIncorrect. -c1 selects only the first character of each line.
cut -d: -f1 accounts.txtCorrect. -d: selects colon separators and -f1 selects the first field.
cut -d: -f2 accounts.txtIncorrect. -f2 selects the second field.
cut -f1 accounts.txtIncorrect. Without -d, cut normally expects tab-separated fields rather than colons.
Try it yourself
An example you can run in a temporary verification environment.
printf 'alice:1000\nbob:1001\n' | cut -d: -f1Expected result
alice
bobKey points
- -d sets the delimiter
- -f selects fields
- -c selects character positions
Notes
- Environment: GNU coreutils 9.x / 標準入力
- Use small, disposable text streams when testing filters.
- Locale, encoding, and GNU/POSIX implementation details can affect edge cases.
Foundation review
Place the operation
Field extraction depends on a consistent delimiter and one-based field selection.
Check the side effect
Include empty and missing fields in a disposable input when verifying behavior.