All mock tests are FREE till 31 December — register to save your progress

Which UNIX command correctly changes the case of only the first three lines of the file "shortlist" from lower case to upper case?

Pick an option to check yourself, or open the answer below.

Show answer & explanation

Correct answer: B. $ head -3 shortlist | tr '[a-z]' '[A-Z]'

Explanation

The tr utility only ever reads from standard input and takes exactly two operands (the two character sets) — it does not accept a filename as a third operand, so any form that feeds tr a filename directly, like option (i)'s "tr '[a-z]' '[A-Z]' shortlist", is invalid. The correct approach is to first extract the required lines and then pipe them into tr: head -3 shortlist sends only the first three lines of the file to standard output, which tr '[a-z]' '[A-Z]' then converts to upper case, giving exactly the desired result. Options (iii) and (iv) jumble the command and argument order into syntactically meaningless invocations. Hence option (ii) is correct.

More Software Engineering questions