Linux Labs

Bash: text processing and scripts / Stage 2 of 9

sed and awk: editing and fields of a stream

Not running Starting usually ~20 s
Leave the lesson

Step 2 of 9 · on a machine · 5 min

Stream, replacement, address

sed changed the address twice in its output, and the file stayed as it was. This step shows why, and how to limit the replacement to the line you need.

sed is a stream editor. It reads a file or standard input line by line, runs its command on every line and prints the result. sed leaves the file itself alone, which is why cat showed the old address.

The s/old/new/ command replaces the first match of the pattern in a line. The pattern is a regular expression, as in grep, and it is searched for anywhere in the line: the address was found both in backend= and in the comment. A g at the end, s/old/new/g, replaces every match in the line.

You can put an address before the command — a pattern between slashes. Then the command works on the lines where the pattern matched, and sed prints the other lines as they are:

bash
sed '/^timeout=/s/30/60/' site-01.conf

The timeout changed, and the lines with the address stayed as they were: none of them starts with timeout=.

Check yourself 1 question

One config has a different backend — backend=10.0.0.50:8080. What happens to that line after sed '/^backend=/s/10\.0\.0\.5/10.0.0.9/'?

  • sed replaced the matching characters, 10.0.0.5, and the 0 after them stayed put: the result is 10.0.0.90.

  • The pattern is searched for anywhere in the line: 10.0.0.5 is the start of 10.0.0.50, and sed replaced it. Check: echo 'backend=10.0.0.50:8080' | sed 's/10\.0\.0\.5/10.0.0.9/'.

  • There is no error: the pattern matched part of the line, and sed replaced that part.

Like in grep, the replacement pattern is searched for anywhere in the line, and 10.0.0.5 matched the start of 10.0.0.50. sed replaced the matching characters and produced someone else's address, 10.0.0.90. The next character in the pattern sets the boundary: s/10\.0\.0\.5:/10.0.0.9:/ leaves the 10.0.0.50 line alone.

Key points

  • sed prints the changed stream and leaves the file as it was.
  • s/old/new/ changes the first match in a line, with g — every match.
  • An address /pattern/ before a command limits it to the lines where the pattern matched.
Ubuntu 24.04
Loading…