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 9 of 9 · summary · 2 min

Summary

  • sed reads a stream line by line and prints the result; only the -i option changes the file, and -i.bak keeps a copy to roll back to.
  • The /^backend=/ address limits the replacement to the lines you need; like in grep, the replacement pattern is searched for anywhere in the line.
  • awk splits a line into fields $1, $2…; -F sets the separator, and END prints the total after the last line.
  • The array s[$2] += $3 adds up a total for every value of a field, and a condition before the action picks the lines.
  • At the start of the lesson, sed without an address changed the rollback line too and wrote nothing to the file: the replacement lacked a line address, and the write lacked the -i option.
Ubuntu 24.04
Loading…