#!/bin/awk -f # # 07/11/2012 Initial Version # 26/01/2015 Added capability for indentation of second and subsequent lines to differ # relative to the first line # 28/01/2015 Solved bug which always caused at least one character indentation BEGIN { line_length = 100; construction_line = ""; } NR == 1 { initial_indent_size = match($0, $1) - 1; indent_string = substr($0, 1, initial_indent_size); construction_line = substr($0, 1, initial_indent_size-1); } NR == 2 { ongoing_indent_size = match($0, $1) - 1; indent_string = substr($0, 1, ongoing_indent_size); } { current_line_content = substr($0, match($0, $1)); for (word = 1; word <= NF; word++) { if (length(construction_line) + length($word) + 1 <= line_length) { if (length(construction_line) == 0) construction_line = $word; else construction_line = construction_line " " $word; } else { print construction_line; construction_line = indent_string $word; } } } END { print construction_line; }