The input string is "apple banana cherry". Awk splits input by spaces and $2 refers to the second word.
Step 2: Identify the second word
The second word is "banana".
Final Answer:
banana -> Option D
Quick Check:
awk $2 = second word = banana [OK]
Hint: awk $2 prints the second word from input [OK]
Common Mistakes:
Confusing $1 and $2 fields
Expecting full line output
Not splitting input by spaces
4. This script is intended to replace all occurrences of 'cat' with 'dog' in a file named pets.txt. What is wrong?
sed 's/cat/dog/g pets.txt'
medium
A. sed cannot replace text
B. File name should come before the command
C. Missing closing single quote after g
D. The command should use double quotes instead of single quotes
Solution
Step 1: Check sed command syntax
The substitution command must be enclosed in quotes fully: 's/cat/dog/g'
Step 2: Identify the error in the given command
The closing single quote is missing after the g, causing a syntax error.
Final Answer:
Missing closing single quote after g -> Option C
Quick Check:
Quotes must enclose entire sed expression [OK]
Hint: Check for matching quotes around sed expressions [OK]
Common Mistakes:
Forgetting to close quotes
Misplacing file name inside quotes
Thinking sed can't replace text
5. You want to extract all lines containing the word error from multiple log files and save them into a single file errors.txt. Which script snippet correctly does this?
hard
A. grep error *.log > errors.txt
B. grep error errors.txt > *.log
C. grep > errors.txt *.log
D. grep *.log error > errors.txt
Solution
Step 1: Understand grep usage with multiple files
Using grep error *.log searches for 'error' in all files ending with .log.
Step 2: Redirect output to a file
The > operator sends all matching lines into errors.txt.
Final Answer:
grep error *.log > errors.txt -> Option A
Quick Check:
grep pattern files > outputfile [OK]
Hint: Use grep pattern files > outputfile to save matches [OK]
Common Mistakes:
Placing > before files
Swapping pattern and files
Using incorrect order of arguments
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.