010-160 Searching and Extracting Data 4 — Questions and Answers
Question 1: Which command would display only lines that do NOT match a pattern in grep?
- grep -m pattern file
- grep -v pattern file (Correct answer)
- grep -x pattern file
- grep -e pattern file
Correct answer: grep -v pattern file
grep -v inverts the match, printing only lines that do not contain the pattern.
Question 2: What is the default field separator used by the `sort` command?
- Comma
- Colon
- Whitespace (Correct answer)
- Tab
Correct answer: Whitespace
By default, sort treats any whitespace (spaces or tabs) as a field separator.
Question 3: Which sed command replaces only the first occurrence of 'foo' with 'bar' on each line?
- sed 's/foo/bar/g' file
- sed 's/foo/bar/' file (Correct answer)
- sed 's/foo/bar/1g' file
- sed 'r/foo/bar/' file
Correct answer: sed 's/foo/bar/' file
Without the g flag, sed s/// replaces only the first match on each line.
Question 4: What does `head -n 20 file.txt` display?
- The last 20 lines of file.txt
- Lines 20 through the end of file.txt
- The first 20 lines of file.txt (Correct answer)
- Lines matching the number 20
Correct answer: The first 20 lines of file.txt
head -n 20 prints the first 20 lines of the specified file.
Question 5: Which command finds all files with the .conf extension under /etc?
- find /etc -name '*.conf' (Correct answer)
- find /etc -type conf
- locate /etc *.conf
- grep -r .conf /etc
Correct answer: find /etc -name '*.conf'
find /etc -name '*.conf' searches /etc for files whose names match the *.conf glob.
Question 6: What does `wc -w file.txt` count?
- Lines in the file
- Words in the file (Correct answer)
- Characters in the file
- Bytes in the file
Correct answer: Words in the file
wc -w counts the number of words (whitespace-delimited tokens) in the file.
Question 7: Which pipe chain counts the number of unique lines in a file?
- sort file | uniq | wc -l (Correct answer)
- uniq file | sort | wc -w
- wc -l file | sort | uniq
- sort -u file | wc -c
Correct answer: sort file | uniq | wc -l
Sorting first groups duplicates together, then uniq removes them, and wc -l counts what remains.
Which command would display only lines that do NOT match a pattern in grep?