Linux Basics Lesson 11 of 38
Changing text with sed
9 minutes
Learning goals
- Replace text in a stream with sed 's/old/new/' and the flag g.
- Print only certain lines with sed -n and an address.
- Change a file in place, safely, with Check first and sed -i.bak.
Replacing: s///
s stands for substitute. Between the slashes come the old text and the new. sed writes the result to the screen and leaves the file
alone:
sed 's/staging/production/' env.conf
Purpose: Search and replace in a stream. The file stays as it is unless you say -i.
Syntax: sed [-n] [-i[SUFFIX]] 'SCRIPT' [FILE...]
Options:
Example:
cat env.conf
API_HOST=staging.example.com
PATHS=/srv/staging:/opt/staging
DEBUG=true
sed 's/staging/production/' env.conf
API_HOST=production.example.com
PATHS=/srv/production:/opt/staging
DEBUG=true
sed 's/staging/production/g' env.conf
API_HOST=production.example.com
PATHS=/srv/production:/opt/production
DEBUG=trueLook at the PATHS line. Without g, only the first match in each line changes. With g (global), all of them do. Forgetting it is one
of the classic ways of half-finishing a replacement.
The pattern is the same kind as for grep, so ^ and $ work here too. sed 's/^/> /' puts > in front of every line.
Printing certain lines: -n
Normally sed prints every line. -n turns that off, and then only what you ask for with p gets printed. In front of the p stands an
address: a pattern between slashes or a line number.
sed -n '/ERROR/p' logs/app.log
09:27:15 ERROR db timeout after 5000ms
09:41:09 ERROR db timeout after 5000ms
sed -n '2,3p' env.conf
PATHS=/srv/staging:/opt/staging
DEBUG=true
sed '/^DEBUG/d' env.conf
API_HOST=staging.example.com
PATHS=/srv/staging:/opt/staging
d works the other way around and drops the addressed lines from the output. Again, the file itself stays as it was.
Changing the file: -i
-i writes the result back into the file, without any question. -i.bak keeps a copy of the old state first, with the ending you name
right behind the i. That copy is your safety net.
sed -i.bak 's/DEBUG=true/DEBUG=false/' env.conf
cat env.conf
API_HOST=staging.example.com
PATHS=/srv/staging:/opt/staging
DEBUG=false
cat env.conf.bak
API_HOST=staging.example.com
PATHS=/srv/staging:/opt/staging
DEBUG=true
Practice
Terminal task 1/3
Show env.conf with every staging replaced by production, also where it comes twice in a line. Do not change the file.
Task 1/3: Show env.conf with every staging replaced by production, also where it comes twice in a line. Do not change the file.
Show env.conf with every staging replaced by production, also where it comes twice in a line. Do not change the file.
Task complete
Solution
sed 's/staging/production/g' env.conf Why: The first pass without -i is your preview. The file stays untouched, so you can look at the result as long as you want.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Terminal task 2/3
Print only the lines of logs/app.log that contain WARN, with sed and not with grep.
Task 2/3: Print only the lines of logs/app.log that contain WARN, with sed and not with grep.
Print only the lines of logs/app.log that contain WARN, with sed and not with grep.
Task complete
Solution
sed -n '/WARN/p' logs/app.log Why: For this alone, grep is shorter. The address form becomes useful once you want to change only certain lines, as in sed '/WARN/s/a/b/'.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Terminal task 3/3
Now for real. Change DEBUG=true to DEBUG=false in env.conf, and keep the old state as env.conf.bak.
Task 3/3: Now for real. Change DEBUG=true to DEBUG=false in env.conf, and keep the old state as env.conf.bak.
Now for real. Change DEBUG=true to DEBUG=false in env.conf, and keep the old state as env.conf.bak.
Task complete
Solution
sed -i.bak 's/DEBUG=true/DEBUG=false/' env.conf Why: There is no space between -i and .bak. With a space, GNU sed takes .bak as the script and the rest goes wrong.
See the solution? You get fewer XP for this task.
Start over? Files, folders and output go back to the start. Your hints stay.
Loading terminal …
Esc, then Tab leaves the terminal
Not done yet
Repeat: you have solved this task before.
Build the command
Build the line that prints only lines 2 and 3 of env.conf.
Your command
Building blocks
Check works once your command has at least one block.
Find the error
This line is supposed to show env.conf with production instead of staging, but the screen stays empty. Tap the part that is wrong.
Parts of the command
Check works once you have tapped a part.
Next up: awk, for when you want to calculate with columns.
Quiz
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
-
Check works once you have chosen an answer.
Cards
-
Card 1 of 3
Tap the card or press Space to turn it over.
-
Card 2 of 3
Tap the card or press Space to turn it over.
-
Card 3 of 3
Tap the card or press Space to turn it over.
End of the lesson
Still open: 5 exercises, 3 quiz questions
What you can do now: sed 's/staging/production/' env.conf
+0 XP in this lesson
- Lesson 1: open
- Lesson 2: open
- Lesson 3: open
- Lesson 4: open
- Lesson 5: open
- Lesson 6: open
- Lesson 7: open
- Lesson 8: open
- Lesson 9: open
- Lesson 10: open
- Lesson 11: done
- Lesson 12: open
- Lesson 13: open
- Lesson 14: open
- Lesson 15: open
- Lesson 16: open
- Lesson 17: open
- Lesson 18: open
- Lesson 19: open
- Lesson 20: open
- Lesson 21: open
- Lesson 22: open
- Lesson 23: open
- Lesson 24: open
- Lesson 25: open
- Lesson 26: open
- Lesson 27: open
- Lesson 28: open
- Lesson 29: open
- Lesson 30: open
- Lesson 31: open
- Lesson 32: open
- Lesson 33: open
- Lesson 34: open
- Lesson 35: open
- Lesson 36: open
- Lesson 37: open
- Lesson 38: open
Cheat sheet
-
sed 's/old/new/g' <file>replace everywhere, file stays the same -
sed -n '/x/p' <file>only the lines with x -
sed '/x/d' <file>everything except the lines with x -
sed -i.bak '...' <file>change the file, keep a copy
The most important command lines of this module on one A4 page, black and white, to print or keep on your phone.
- Download PDF (PDF, 20 KB)
- View as image (PNG, 73 KB)