Linux Text Processing & Editors 1 — Questions and Answers
Question 1: Which vim command saves the current file and exits the editor?
- :q!
- :wq (Correct answer)
- :x!
- :save
Correct answer: :wq
:wq writes (saves) the file and then quits vim in a single command.
Question 2: What does the `grep -i` flag do?
- Inverts the match to show non-matching lines
- Performs a case-insensitive search (Correct answer)
- Displays line numbers alongside matches
- Searches recursively through directories
Correct answer: Performs a case-insensitive search
The -i flag tells grep to ignore case differences when matching patterns, so 'Hello' matches 'hello'.
Question 3: Which sed command replaces ALL occurrences of 'foo' with 'bar' on each line?
- sed 's/foo/bar/' file
- sed 's/foo/bar/g' file (Correct answer)
- sed 'r/foo/bar/' file
- sed '/foo/bar/g' file
Correct answer: sed 's/foo/bar/g' file
The 'g' flag at the end of the sed substitution command makes it replace every occurrence on each line, not just the first.
Question 4: What does `awk '{print $2}' file` output?
- The entire second line of the file
- The second field (column) of each line (Correct answer)
- Lines that contain exactly 2 fields
- The file content starting from line 2
Correct answer: The second field (column) of each line
In awk, $2 refers to the second whitespace-delimited field of each input record.
Question 5: Which command displays the last 10 lines of a file by default?
- head
- more
- tail (Correct answer)
- less
Correct answer: tail
The tail command shows the last 10 lines of a file by default; use -n to specify a different count.
Question 6: What does `sort -n` do differently from `sort` without flags?
- Sorts output in reverse order
- Sorts by numeric value instead of lexicographically (Correct answer)
- Sorts and removes duplicate lines
- Sorts while ignoring case
Correct answer: Sorts by numeric value instead of lexicographically
The -n flag causes sort to compare values as numbers, so '10' correctly sorts after '9' rather than before it.
Question 7: Which command counts lines, words, and bytes in a file?
- count
- len
- wc (Correct answer)
- size
Correct answer: wc
The wc (word count) command reports the number of lines, words, and bytes in a file by default.
Which vim command saves the current file and exits the editor?