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.
Incorrect. -c1 selects only the first character of each line.
cut -d: -f1 accounts.txtCorrect. -d: selects colon separators and -f1 selects the first field.
Correct. -d: selects colon separators and -f1 selects the first field.
cut -d: -f2 accounts.txtIncorrect. -f2 selects the second field.
Incorrect. -f2 selects the second field.
cut -f1 accounts.txtIncorrect. Without -d, cut normally expects tab-separated fields rather than colons.
Incorrect. 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 / 標準入力
- Command output formatting can vary slightly by distribution or tool version.
- Run the example in a temporary directory or process when possible.
Foundation review
Read the scope first
Check whether the command acts on the current shell, a new process, an existing process, or a file.
Verify the observable result
Use the supplied command and compare the output with the expected result.