#!/usr/bin/perl use warnings; use strict; my $input = << "EOF"; foo\tbar\t\tbaz\tbooz\t\tqaaz\t\t\tabc foo\t\tbar\tbaz\t\tbooz\tqaaz\tabc\t123 foo\t\tbar\t\tbaz\t\tbooz\tqaaz\t\tabc EOF open my $in, '<', \$input or die $!; my @tab_counts; while (<$in>) { my $i = 0; for my $tab_count (map length, /(\t+)/g) { $tab_counts[$i] = $tab_count if $tab_count > ($tab_counts[$i] || 0); ++$i; } } push @tab_counts, 0; # No tab after the last field. seek $in, 0, 0; while (<$in>) { my $i = 0; print $_, "\t" x $tab_counts[$i++] for /\S+/g; print "\n"; }