LFCS · Question #855
Which of the following commands prints a list of usernames (first column) and their primary group (fourth column) from the /etc/passwd file?
The correct answer is C. cut -d : -f 1,4 /etc/passwd. To extract specific columns from a delimited text file like /etc/passwd, the cut command is the appropriate tool.
Question
Options
- Afmt -f 1,4 /etc/passwd
- Bsplit -c 1,4 /etc/passwd
- Ccut -d : -f 1,4 /etc/passwd
- Dpaste -f 1,4 /etc/passwd
How the community answered
(34 responses)- B3% (1)
- C91% (31)
- D6% (2)
Why each option
To extract specific columns from a delimited text file like `/etc/passwd`, the `cut` command is the appropriate tool.
The `fmt` command is used for reformatting text to fit a specified width, not for extracting specific columns from delimited data.
The `split` command divides a file into smaller, separate files, which is not the function required to extract specific columns.
The `cut` command with `-d :` specifies the colon as the field delimiter for `/etc/passwd`, and `-f 1,4` then extracts the first field (username) and the fourth field (GID, representing the primary group), providing the requested information.
The `paste` command is used to merge lines from multiple files horizontally, or to combine fields from a single file with a specified delimiter, but `cut` is specifically for extracting fields.
Concept tested: Parsing /etc/passwd with cut
Source: https://man7.org/linux/man-pages/man1/cut.1.html
Topics
Community Discussion
No community discussion yet for this question.