terminal.cab

Linux Basics Lesson 11 of 38

guestLevel 1Badges and progress0/100 XP, XP to the next levelStreak0

Changing text with sed

9 minutes

Learning goals

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:

  • s/old/new/g replace all matches per line, without g only the first
  • -n '/x/p' print only the lines that contain x
  • -i.bak change the file itself and keep a copy as .bak

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=true

in the glossary: sed

Look 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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

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.

Loading terminal …

lerner@web-01: ~bash
lerner@web-01:~$

Esc, then Tab leaves the terminal

Not done yet

Build the command

Build the line that prints only lines 2 and 3 of env.conf.

Your command

    Tap the blocks below to build the 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

    1. Question 1 of 3A line reads a a a. What does sed 's/a/b/' turn it into?

      Check works once you have chosen an answer.

    2. Question 2 of 3You ran sed 's/a/b/' app.conf. What has happened to app.conf?

      Check works once you have chosen an answer.

    3. Question 3 of 3sed -i 's/a/b/' file fails on a Mac. Why?

      Check works once you have chosen an answer.

    Cards

    1. Card 1 of 3

      Tap the card or press Space to turn it over.

    2. Card 2 of 3

      Tap the card or press Space to turn it over.

    3. 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

    Next: lesson 12, Evaluating columns with awk (8 min)

    What you can do now: sed 's/staging/production/' env.conf

    +0 XP in this lesson

    1. Lesson 1: open
    2. Lesson 2: open
    3. Lesson 3: open
    4. Lesson 4: open
    5. Lesson 5: open
    6. Lesson 6: open
    7. Lesson 7: open
    8. Lesson 8: open
    9. Lesson 9: open
    10. Lesson 10: open
    11. Lesson 11: done
    12. Lesson 12: open
    13. Lesson 13: open
    14. Lesson 14: open
    15. Lesson 15: open
    16. Lesson 16: open
    17. Lesson 17: open
    18. Lesson 18: open
    19. Lesson 19: open
    20. Lesson 20: open
    21. Lesson 21: open
    22. Lesson 22: open
    23. Lesson 23: open
    24. Lesson 24: open
    25. Lesson 25: open
    26. Lesson 26: open
    27. Lesson 27: open
    28. Lesson 28: open
    29. Lesson 29: open
    30. Lesson 30: open
    31. Lesson 31: open
    32. Lesson 32: open
    33. Lesson 33: open
    34. Lesson 34: open
    35. Lesson 35: open
    36. Lesson 36: open
    37. Lesson 37: open
    38. Lesson 38: open
    1 of 38

    Next: lesson 12, Evaluating columns with awk (8 min) That's enough for today

    Path complete

    You finished all 38 lessons of Linux Basics.

    Back to the path

    Cheat sheet

    The most important command lines of this module on one A4 page, black and white, to print or keep on your phone.