Linux Text Processing & Editors 2 — Questions and Answers
Question 1: What does `cut -d: -f1 /etc/passwd` do?
- Removes the first field from /etc/passwd
- Extracts the first colon-delimited field from each line (Correct answer)
- Counts the number of colons in /etc/passwd
- Displays only lines that start with a colon
Correct answer: Extracts the first colon-delimited field from each line
cut -d: sets the delimiter to colon and -f1 extracts the first field, which is the username in /etc/passwd.
Question 2: Which command translates all lowercase letters to uppercase from stdin?
- tr 'a-z' 'A-Z' (Correct answer)
- case upper
- upper file
- convert -u file
Correct answer: tr 'a-z' 'A-Z'
The tr command translates characters; 'a-z' specifies the source character range and 'A-Z' specifies the destination range.
Question 3: What does `uniq -c` do?
- Removes all duplicate lines from the input
- Prefixes each line with the count of consecutive occurrences (Correct answer)
- Shows only unique lines with no adjacent duplicates
- Checks if an entire file has unique content
Correct answer: Prefixes each line with the count of consecutive occurrences
uniq -c prefixes each output line with the number of times that consecutive line appeared in the input.
Question 4: What does the `tee` command do?
- Compresses output into a tarball
- Reads from stdin and writes to both stdout and a file simultaneously (Correct answer)
- Splits a file into two equal halves
- Creates a hard link to a file
Correct answer: Reads from stdin and writes to both stdout and a file simultaneously
tee reads from standard input and writes simultaneously to standard output and to one or more files, like a T-shaped pipe fitting.
Question 5: Which command shows the differences between two text files line by line?
- cmp
- compare
- delta
- diff (Correct answer)
Correct answer: diff
diff compares two files line by line and shows exactly what changes would make the first file identical to the second.
Question 6: What does `sed -n '5,10p'` do when applied to a file?
- Deletes lines 5 through 10 from the file
- Prints only lines 5 through 10 to stdout (Correct answer)
- Substitutes text only between lines 5 and 10
- Counts the lines between 5 and 10
Correct answer: Prints only lines 5 through 10 to stdout
sed -n suppresses automatic printing, and '5,10p' explicitly prints only lines 5 through 10.
Question 7: What does the `paste` command do?
- Appends one file's content to the end of another
- Merges lines from multiple files side by side separated by tabs (Correct answer)
- Copies clipboard content into a file
- Concatenates files end-to-end like cat
Correct answer: Merges lines from multiple files side by side separated by tabs
paste merges corresponding lines from multiple files horizontally, separating them with tab characters by default.
What does `cut -d: -f1 /etc/passwd` do?