0
0
Bash Scriptingscripting~10 mins

sed for substitution in scripts in Bash Scripting - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
As/apple/orange/g
Bd/apple/orange/g
Cr/apple/orange/g
Dx/apple/orange/g
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'.
2fill in blank
medium

Complete 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'
As/cat/dog/g
Bs/cat/dog/1
Cs/cat/dog/
Ds/cat/dog/2
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.
3fill in blank
hard

Fix 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'
As/blue/red
Bs/blue/red/g
Cs/blue/red/g;
Ds/blue/red/g//
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.
4fill in blank
hard

Fill 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'
Apet
Bdog
Ccat
Danimal
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.
5fill in blank
hard

Fill 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'
Afruit
Bapple
Cbanana
Dorange
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'.