#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11140114 use warnings; 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 print "$input\n"; open my $in, '<', \$input or die $!; my @tabs; while( <$in> ) { my $index = 0; $tabs[$index++] |= $& while /\t+/g; } seek $in, 0, 0; while( <$in> ) { my $index = 0; print s/\t+/$tabs[$index++]/gr; }