awk: Indent with awk

icalendar content has logically nested blocks starting and ending on a new line but without any indentation. A little awk script can add some indentation to make it easier to read.

Original icalendar.ics:

BEGIN:VCALENDAR
... Calendar properties
BEGIN:TZINFO
... Timezone properties
END:TZINFO
BEGIN:VEVENT
... Event properties
END:VEVENT
BEGIN:VEVENT
... Event properties
END:VEVENT
END:VCALENDAR

Little indent.awk script to add indentation:

/^END:/   { prefix = substr(prefix, 3) }
          { print prefix $0 }
/^BEGIN:/ { prefix = prefix "  " }

Result of awk --file indent.awk icalendar.ics:

BEGIN:VCALENDAR
  ... Calendar properties
  BEGIN:TZINFO
    ... Timezone properties
  END:TZINFO
  BEGIN:VEVENT
    ... Event properties
  END:VEVENT
  BEGIN:VEVENT
    ... Event properties
  END:VEVENT
END:VCALENDAR
Published on: 11 Jul 2022