Bird
Raised Fist0
Bash Scriptingscripting~5 mins

sed for substitution in scripts in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does the sed command do in a script?

sed is a tool that edits text by searching and replacing patterns. It reads input line by line and can change text automatically.

Click to reveal answer
beginner
How do you write a basic substitution command with sed?

The basic form is sed 's/old/new/'. It means: find old and replace it with new in each line.

Click to reveal answer
beginner
What does the g flag do in sed 's/old/new/g'?

The g flag means "global". It replaces all occurrences of old in each line, not just the first one.

Click to reveal answer
intermediate
How can you use sed to replace text in a file and save the changes back to the file?

Use the -i option: sed -i 's/old/new/g' filename. This edits the file directly.

Click to reveal answer
intermediate
Why is it useful to use different delimiters in sed substitution, like sed 's|old|new|g'?

Using different delimiters helps when the text contains slashes /. It avoids confusion and makes the command easier to read.

Click to reveal answer
What does the command sed 's/apple/orange/' do?
AAdds 'orange' after 'apple'
BDeletes all 'apple' words
CReplaces the first 'apple' with 'orange' in each line
DPrints lines containing 'apple'
How do you replace all occurrences of 'cat' with 'dog' in a file named pets.txt and save changes directly?
Ased -i 's/cat/dog/g' pets.txt
Bsed 's/cat/dog/g' pets.txt
Csed 's/cat/dog/' pets.txt > pets.txt
Dsed -r 's/cat/dog/g' pets.txt
Why might you use sed 's|path/to/old|path/to/new|g' instead of sed 's/path\/to\/old/path\/to\/new/g'?
ABecause <code>|</code> is faster
BTo avoid escaping slashes <code>/</code> in paths
CBecause <code>/</code> is not allowed in sed
DTo make the command case-insensitive
What happens if you run sed 's/old/new/' without the g flag?
AThe command deletes the line
BAll occurrences of 'old' are replaced
CNo replacement happens
DOnly the first occurrence of 'old' per line is replaced
Which option edits a file without creating a backup when using sed -i?
Ased -i 's/old/new/' file
Bsed -i '' 's/old/new/' file
Csed -i.bak 's/old/new/' file
Dsed 's/old/new/' file
Explain how to use sed to replace all occurrences of a word in a text file and save the changes.
Think about the 's/old/new/g' pattern and how to apply it directly to a file.
You got /4 concepts.
    Describe why choosing different delimiters in sed substitution commands can be helpful.
    Consider when your text contains slashes or other special characters.
    You got /4 concepts.

      Practice

      (1/5)
      1. What does the sed command sed 's/cat/dog/' file.txt do?
      easy
      A. Prints lines containing 'cat' from file.txt
      B. Replaces all occurrences of 'cat' with 'dog' in the entire file.txt
      C. Replaces the first occurrence of 'cat' with 'dog' on each line of file.txt
      D. Deletes all lines containing 'cat' from file.txt

      Solution

      1. Step 1: Understand the sed substitution syntax

        The command sed 's/cat/dog/' file.txt uses s/old/new/ which replaces only the first match per line.
      2. Step 2: Apply to the file content

        Each line in file.txt will have its first 'cat' replaced by 'dog', other occurrences remain unchanged.
      3. Final Answer:

        Replaces the first occurrence of 'cat' with 'dog' on each line of file.txt -> Option C
      4. Quick Check:

        sed s/old/new/ replaces first match per line [OK]
      Hint: Remember: no 'g' means only first match replaced per line [OK]
      Common Mistakes:
      • Thinking it replaces all matches without 'g'
      • Confusing substitution with deletion
      • Assuming it prints only matching lines
      2. Which of the following is the correct syntax to replace all occurrences of 'apple' with 'orange' in a file using sed?
      easy
      A. sed 's/apple/orange/' file.txt
      B. sed 'replace apple with orange' file.txt
      C. sed 's/apple/orange//g' file.txt
      D. sed 's/apple/orange/g' file.txt

      Solution

      1. Step 1: Identify the global flag usage

        To replace all occurrences on each line, the 'g' flag is needed after the substitution pattern.
      2. Step 2: Check each option

        sed 's/apple/orange/g' file.txt uses s/apple/orange/g which correctly replaces all 'apple' with 'orange'. sed 's/apple/orange/' file.txt replaces only first occurrence. sed 'replace apple with orange' file.txt is invalid syntax. sed 's/apple/orange//g' file.txt has extra slashes causing syntax error.
      3. Final Answer:

        sed 's/apple/orange/g' file.txt -> Option D
      4. Quick Check:

        Use 'g' flag for all matches [OK]
      Hint: Add 'g' after last slash to replace all matches [OK]
      Common Mistakes:
      • Omitting the 'g' flag to replace all matches
      • Adding extra slashes causing syntax errors
      • Using incorrect command words like 'replace'
      3. What will be the output of the following command if the input file contains the line apple apple apple?
      sed 's/apple/orange/' file.txt
      medium
      A. orange orange orange
      B. orange apple apple
      C. apple orange apple
      D. apple apple orange

      Solution

      1. Step 1: Understand sed substitution without 'g'

        Without the 'g' flag, sed replaces only the first occurrence of 'apple' per line.
      2. Step 2: Apply substitution to the input line

        The first 'apple' becomes 'orange', the rest remain unchanged, resulting in 'orange apple apple'.
      3. Final Answer:

        orange apple apple -> Option B
      4. Quick Check:

        First match replaced only without 'g' [OK]
      Hint: No 'g' means only first match changes per line [OK]
      Common Mistakes:
      • Assuming all occurrences change without 'g'
      • Confusing order of words after substitution
      • Thinking sed changes input file by default
      4. You wrote this sed command to replace 'foo' with 'bar' globally: sed 's/foo/bar/' file.txt. But only the first 'foo' per line is replaced. What is the fix?
      medium
      A. Add the 'g' flag: sed 's/foo/bar/g' file.txt
      B. Use double quotes instead of single quotes
      C. Add a space after 'bar/'
      D. Use sed -i option to edit in place

      Solution

      1. Step 1: Identify missing global flag

        The command lacks the 'g' flag, so only first occurrence per line is replaced.
      2. Step 2: Add the 'g' flag to fix

        Adding 'g' after the substitution pattern makes sed replace all occurrences per line.
      3. Final Answer:

        Add the 'g' flag: sed 's/foo/bar/g' file.txt -> Option A
      4. Quick Check:

        Global flag 'g' replaces all matches [OK]
      Hint: Add 'g' to replace all matches, not just first [OK]
      Common Mistakes:
      • Thinking quotes affect substitution behavior
      • Adding spaces inside substitution pattern
      • Confusing in-place edit with substitution flags
      5. You want to replace the word 'error' with 'warning' only on lines containing 'log' in a file. Which sed command achieves this?
      hard
      A. sed '/log/ s/error/warning/g' file.txt
      B. sed 's/error/warning/g /log/' file.txt
      C. sed 's/error/warning/g if /log/' file.txt
      D. sed 's/error/warning/g' file.txt | grep log

      Solution

      1. Step 1: Understand conditional substitution in sed

        To apply substitution only on lines matching a pattern, use /pattern/ s/old/new/flags syntax.
      2. Step 2: Analyze options

        sed '/log/ s/error/warning/g' file.txt correctly applies substitution only on lines containing 'log'. Options A and C have invalid syntax. sed 's/error/warning/g' file.txt | grep log replaces everywhere then filters lines, which does not limit substitution.
      3. Final Answer:

        sed '/log/ s/error/warning/g' file.txt -> Option A
      4. Quick Check:

        Use address before s/// to limit lines [OK]
      Hint: Put pattern before s/// to restrict substitution lines [OK]
      Common Mistakes:
      • Placing substitution after pattern incorrectly
      • Trying to use if statements inside sed
      • Filtering lines after substitution instead of limiting it
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard