0
0
Linux CLIscripting~5 mins

sed substitution in Linux CLI - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the sed command do in Linux?

sed is a stream editor used to perform basic text transformations on an input stream (a file or input from a pipeline).

Click to reveal answer
beginner
Explain the basic syntax of a sed substitution command.

The basic syntax is sed 's/pattern/replacement/' filename. It replaces the first occurrence of pattern with replacement on each line.

Click to reveal answer
beginner
How do you replace all occurrences of a pattern in each line using sed?

Add the g flag at the end: sed 's/pattern/replacement/g' filename. This replaces every match on each line.

Click to reveal answer
beginner
What does the command sed 's/cat/dog/g' file.txt do?

It replaces every occurrence of the word cat with dog in each line of file.txt, printing the result to the screen.

Click to reveal answer
intermediate
How can you save the changes made by sed substitution back to the original file?

Use the -i option for in-place editing: sed -i 's/old/new/g' filename. This updates the file directly.

Click to reveal answer
What does the g flag do in a sed substitution?
AReplaces only the first occurrence in the entire file
BDeletes the matching lines
CReplaces all occurrences of the pattern in each line
DPrints the matching lines only
Which command replaces the first occurrence of 'apple' with 'orange' in each line of fruits.txt?
Ased 's/apple/orange/' fruits.txt
Bsed 's/apple/orange/g' fruits.txt
Csed -i 's/apple/orange/g' fruits.txt
Dsed 's/apple/orange/1' fruits.txt
How do you apply sed substitution and save changes directly to the file?
Ased -i 's/old/new/g' filename
Bsed 's/old/new/g' filename > filename
Csed 's/old/new/g' filename
Dsed -e 's/old/new/g' filename
What will this command do? sed 's/blue/red/2' colors.txt
AReplace the second line containing 'blue' with 'red'
BReplace all occurrences of 'blue' with 'red'
CReplace the first occurrence of 'blue' with 'red'
DReplace the second occurrence of 'blue' with 'red' on each line
Which delimiter can you use instead of / in sed substitution?
A*
B#
C&
D%
Describe how to use sed to replace all occurrences of a word in a file and save the changes.
Think about the command structure and flags needed.
You got /3 concepts.
    Explain the difference between using sed 's/pattern/replacement/' and sed 's/pattern/replacement/g'.
    Focus on how many matches get replaced per line.
    You got /3 concepts.