Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to replace 'apple' with 'orange' in the file 'fruits.txt'.
Bash Scripting
sed '[1]' fruits.txt
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'r' instead of 's' for substitution.
Forgetting the 'g' flag to replace all occurrences.
Using incorrect command letters like 'd' or 'x'.
✗ Incorrect
The 's' command in sed is used for substitution. 's/apple/orange/g' replaces all occurrences of 'apple' with 'orange'.
2fill in blank
mediumComplete the code to replace only the first occurrence of 'cat' with 'dog' in each line of 'pets.txt'.
Bash Scripting
sed '[1]' pets.txt
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Adding 'g' flag replaces all occurrences, not just the first.
Using numbers like '/1' or '/2' which are not valid flags here.
✗ Incorrect
Without the 'g' flag, sed replaces only the first occurrence per line. So 's/cat/dog/' replaces the first 'cat' with 'dog'.
3fill in blank
hardFix the error in the sed command to replace 'blue' with 'red' in 'colors.txt'.
Bash Scripting
sed '[1]' colors.txt
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Adding a semicolon at the end causes syntax error.
Adding extra slashes after 'g' causes errors.
Omitting the 'g' flag replaces only first occurrence.
✗ Incorrect
The correct sed substitution command is 's/blue/red/g' without extra characters. It replaces all 'blue' with 'red'.
4fill in blank
hardFill both blanks to create a sed command that replaces 'dog' with 'cat' only on lines containing 'pet'.
Bash Scripting
sed -n '/[1]/ s/[2]/cat/gp' animals.txt
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'cat' as the pattern to match lines instead of 'pet'.
Replacing 'cat' with 'dog' instead of the other way around.
Omitting the 'p' flag so lines are not printed.
✗ Incorrect
The command '/pet/ s/dog/cat/gp' means: on lines matching 'pet', substitute 'dog' with 'cat' globally and print those lines.
5fill in blank
hardFill all three blanks to create a sed command that replaces 'apple' with 'banana' only on lines containing 'fruit' and saves output to 'new.txt'.
Bash Scripting
sed -n '/[1]/ s/[2]/[3]/gp' fruits.txt > new.txt
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong pattern to match lines.
Swapping 'apple' and 'banana' in substitution.
Forgetting to redirect output to 'new.txt'.
✗ Incorrect
The command '/fruit/ s/apple/banana/gp' means: on lines with 'fruit', replace 'apple' with 'banana' globally and print those lines. Redirect output to 'new.txt'.