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 1 of 9 · on a machine · 5 min

Thirty configs

What you will learn

  • Change text with the sed s command and limit the replacement with an address — a line pattern.
  • Edit files in place with sed -i.bak and check the result with grep and diff.
  • Add up a column of numbers in awk and total it for every value of a field, setting the separator with -F.

The sites' backend is moving from 10.0.0.5 to 10.0.0.9. The ~/sed-lab/conf directory holds thirty configs, and each has the address twice: on the backend= line and on the # rollback: line. The rollback line is not to be changed — it brings the old address back if the move fails. Opening thirty files in an editor takes a while, and a colleague suggests a single sed command. Run it on the first file.

Exercise · look

Run your colleague's replacement

In ~/sed-lab/conf, run your colleague's replacement on site-01.conf, then look at the file itself.

First, predict

What will sed print?

Now start the exercise, run the command and compare your prediction with the output.

Sign in and start The machine is available after signing in
Check yourself 1 question

If your colleague's replacement were written to all thirty files, which address would the rollback lines point to?

  • sed deleted no lines: the rollback line is still in its output, with the new address.

  • In sed's output the rollback line already has 10.0.0.9: the replacement went through it too.

  • The s command puts the new text in place of the match: the rollback line in the output has one address, 10.0.0.9.

The rollback line would point to the new server, leaving nowhere to roll back to.

Ubuntu 24.04
Loading…