010-160 Searching and Extracting Data 2 — Questions and Answers
Question 1: Which grep option makes the search case-insensitive?
- -c
- -i (Correct answer)
- -v
- -n
Correct answer: -i
The -i option tells grep to ignore case differences when matching patterns.
Question 2: What does the `cut -d: -f1` command do when processing /etc/passwd?
- Prints the third field using colon as delimiter
- Prints the first field using colon as delimiter (Correct answer)
- Deletes lines containing colons
- Counts lines with colons
Correct answer: Prints the first field using colon as delimiter
cut -d: sets the delimiter to colon and -f1 selects the first field from each line.
Question 3: Which command would you use to sort a file's lines in reverse alphabetical order?
- sort -a file
- sort -r file (Correct answer)
- sort -d file
- sort -z file
Correct answer: sort -r file
The sort -r option reverses the default sort order, producing reverse alphabetical output.
Question 4: What is the purpose of the `wc -l` command?
- Count words in a file
- Count lines in a file (Correct answer)
- Count characters in a file
- Count bytes in a file
Correct answer: Count lines in a file
wc -l counts the number of newline-terminated lines in the input.
Question 5: Which sed command deletes all lines containing the word 'error'?
- sed 's/error//' file
- sed '/error/d' file (Correct answer)
- sed 'error=d' file
- sed -d 'error' file
Correct answer: sed '/error/d' file
In sed, /pattern/d means 'delete lines that match the pattern'.
Question 6: What does `grep -c 'pattern' file` output?
- Lines matching the pattern with line numbers
- The count of matching lines (Correct answer)
- Characters matching the pattern
- A colorized output of matches
Correct answer: The count of matching lines
grep -c prints only the count of lines that match the given pattern.
Question 7: Which command extracts characters 3 through 7 from each line of a file?
- cut -f3-7 file
- cut -c3-7 file (Correct answer)
- cut -b3,7 file
- cut -d3-7 file
Correct answer: cut -c3-7 file
cut -c uses character positions; -c3-7 selects characters 3 through 7 from each line.
Which grep option makes the search case-insensitive?