010-160 Searching and Extracting Data 5 — Questions and Answers
Question 1: Which grep option uses extended regular expressions without needing to escape special characters like `+` or `|`?
- grep -P
- grep -E (Correct answer)
- grep -F
- grep -x
Correct answer: grep -E
grep -E (or egrep) enables extended regular expressions, allowing unescaped use of +, |, (, and ).
Question 2: What does `find . -type d` search for?
- Regular files only
- Symbolic links only
- Directories only (Correct answer)
- Device files only
Correct answer: Directories only
The -type d predicate restricts find results to directories.
Question 3: Which command sorts a file numerically by the second field, using a comma as delimiter?
- sort -t, -k2 -n file (Correct answer)
- sort -d, -f2 -n file
- sort -t, -k2 file
- sort -n -c, -k2 file
Correct answer: sort -t, -k2 -n file
sort -t, sets comma as delimiter, -k2 selects the second field, and -n sorts numerically.
Question 4: What is the effect of `tr -d '\n'` on its input?
- Inserts newlines between every character
- Deletes all newline characters from the input (Correct answer)
- Translates newlines to spaces
- Counts the number of newlines
Correct answer: Deletes all newline characters from the input
tr -d deletes every occurrence of the specified character; here it removes all newline characters.
Question 5: Which command finds files that were accessed more than 7 days ago?
- find . -mtime +7
- find . -atime +7 (Correct answer)
- find . -ctime +7
- find . -newer +7
Correct answer: find . -atime +7
find -atime +7 finds files whose last access time was more than 7 days ago.
Question 6: What does the `grep -l 'error' *.log` command output?
- Every matching line from all .log files
- The count of matches in each .log file
- Only the names of .log files that contain 'error' (Correct answer)
- Lines not containing 'error' in .log files
Correct answer: Only the names of .log files that contain 'error'
grep -l prints only the filenames of files that contain at least one match, not the matching lines.
Question 7: Which pipeline lists the ten most frequently occurring words in a text file?
- cat file | sort | uniq -c | sort -rn | head -10 (Correct answer)
- cat file | uniq | sort -n | head -10
- cat file | wc -w | sort -r | head -10
- cat file | grep -c . | sort | head -10
Correct answer: cat file | sort | uniq -c | sort -rn | head -10
Splitting into words, counting with uniq -c, reverse-numeric sorting, then head -10 gives the top 10 most frequent words.
Which grep option uses extended regular expressions without needing to escape special characters like `+` or `|`?