LPIC-1 Exam 101 free question 5 / 10 · Domain 3: GNU and Unix Commands
users.txt contains the following line. What is the correct output of cut -d: -f6,1 users.txt? aki:x:1001:1001:Aki Sato:/home/aki:/bin/bash
- aki:/home/aki
- /home/aki aki
- /home/aki:aki
- aki /home/aki
Show the answer and explanation
Answer: A
Why this is correct
-f of cut specifies the numbers of the fields to extract and -d specifies the character that separates fields. What is extracted comes out once each in the order it appears in the input, regardless of the order of the numbers you specified. So even if you write -f6,1, field 1, aki, comes first and field 6, /home/aki, comes after it. If you do not specify an output delimiter, it is the same : as the input delimiter.
Why the other options are wrong
"aki /home/aki" … The order is right, but the output delimiter stays the : used on input. To print it separated by a space, specify it with --output-delimiter.
"/home/aki:aki" … That assumes that fields come out in the order of the numbers listed in -f. cut prints in the order the input was read, so writing the numbers in reverse does not swap the order.
"/home/aki aki" … Both the order and the delimiter are wrong. If you want to reorder, use not cut but a tool such as awk that lets you specify the order.
Good to remember
Field numbers in -f start at 1 (there is no field 0). A range can be written as 1-3 or 6-, and numbers can be listed with a comma as in 1,6. A line that does not contain the delimiter character is printed as it is unless you give -s, so be careful with files that mix in headings or blank lines.
Go to the LPIC-1 Exam 101 practice app (10 free questions; purchase unlocks all questions)