>_ IzzyLab

Hands-on task

This task is step 3 of the course “sed: the stream editor”

sed: collapse an aligned table

easy~3 min

/root/ports.txt is a port table from a report: the columns are aligned with spaces, several of them between values. The import needs exactly one space between columns — replace every run of spaces with one and save it to /root/ports.single.txt.

What you'll practise:
  • sed -E
  • + quantifier
  • g flag
  • collapsing repeats

Topic breakdown

Collapsing repeats is a substitution with "a character one or more times" on the left and one such character on the right. The + quantifier ("one or more") matters here: * ("zero or more") also matches the empty string between any two characters. And the g flag is needed because s without it changes only the first match in the line, so in a table with several columns the other runs of spaces survive.

What's checked

  1. no runs of spaces left
  2. table preserved

What it looks like