>_ IzzyLab

Hands-on task

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

sed: backup log dates to ISO

medium~5 min

The backup script writes dates to /root/backup.log in the US format MM/DD/YYYY (month first), for example backup ok 10/23/2026 03:00. Convert all dates to YYYY-MM-DD, leaving the rest of each line as is, and write the result to /root/backup_iso.log.

What you'll practise:
  • sed -E
  • capture groups
  • backreferences
  • {n} counter
  • date format

Topic breakdown

Rearranging the parts of a date is a substitution with capture groups: the numbers go in parentheses, and in the replacement they are output by number in the new order. Groups are numbered by their opening parentheses from left to right, so for MM/DD/YYYY the month is group one, the day group two, the year group three. When the pattern has many /, it's handier to give the s command a different delimiter (s#…#…#). Without anchors the substitution changes the date anywhere in the line, leaving the rest alone.

What's checked

  1. dates in ISO, the rest of the text in place

What it looks like