use strict; use warnings; ; print ''.(chomp, s/\s*$/ /, $_).($_ = , s/^\s*(?=\S)//, $_) while ; __DATA__ Header line First line part 1. Part 2 of first line. Second line parts one and two. Third line - second part empty. Fourth and last line