0
0
Linux CLIscripting~5 mins

sed (stream editor) basics in Linux CLI - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is sed used for in Linux?

sed is a tool to find and change text in files or input streams automatically. It works like a robot that reads text line by line and edits it based on rules you give.

Click to reveal answer
beginner
How do you replace the first occurrence of 'apple' with 'orange' in each line using sed?

Use: sed 's/apple/orange/' filename. This changes only the first 'apple' in each line to 'orange'.

Click to reveal answer
intermediate
What does the -n option do in sed?

The -n option tells sed to not print anything by default. You then use the p command to print only the lines you want.

Click to reveal answer
beginner
Explain the command: sed '2d' filename

This command deletes the second line from the file output. It reads the file and skips printing line 2.

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

Use the -i option: sed -i 's/old/new/g' filename. This edits the file in place without showing output on screen.

Click to reveal answer
What does the command sed 's/cat/dog/' file.txt do?
AReplaces the first 'cat' with 'dog' in each line
BDeletes all lines containing 'cat'
CPrints lines containing 'dog'
DReplaces all 'cat' with 'dog' in the file
Which option prevents sed from printing all lines by default?
A-n
B-e
C-i
D-p
How do you delete the 3rd line of a file using sed?
Ased 'delete 3' filename
Bsed 'd3' filename
Csed -n '3d' filename
Dsed '3d' filename
What does the g flag do in sed 's/old/new/g'?
ADeletes the line if match found
BChanges only the first match per line
CChanges all matches in each line
DPrints the line if match found
How do you edit a file directly with sed?
Ased -n 's/a/b/' file
Bsed -i 's/a/b/' file
Csed 's/a/b/' file > newfile
Dsed 's/a/b/' file -o
Describe how to use sed to replace text in a file and save the changes directly.
Think about the substitution syntax and how to apply changes to the file itself.
You got /3 concepts.
    Explain how sed processes input and how you can control what lines get printed.
    Consider how <code>sed</code> normally prints all lines and how to change that.
    You got /3 concepts.