Unix command to change the case of first three lines of file “shortlist” from…
2017
Unix command to change the case of first three lines of file “shortlist” from lower to upper
- A.
$ tr '[a-z]' '[A-Z]' shortlist ¦ head – 3
- B.
$ head – 3 shortlist ¦ tr ‘[a-z]’ ‘[A-Z]’
- C.
$ tr head – 3 shortlist ‘[A-Z]’ ‘[a-z]’
- D.
$tr shortlist head – 3 ‘[a-z]’ ‘[A-Z]’
Attempted by 31 students.
Show answer & explanation
Correct answer: B
Correct command: head -n 3 shortlist | tr '[:lower:]' '[:upper:]'
Explanation:
Use head -n 3 shortlist to select the first three lines from the file.
Pipe the output (using |) into tr to translate characters from lowercase to uppercase: tr '[:lower:]' '[:upper:]'.
tr reads from standard input and does not take a filename argument; that is why head is used before tr in the pipeline.
Prefer POSIX character classes '[:lower:]' and '[:upper:]' for portability. Also ensure you use the ASCII hyphen (-) for options (e.g., -n 3) and the ASCII pipe (|).