#!/usr/bin/perl # This script translates white space to tabs, # if they come in groups of four. It takes one argument: 'text file'. use 5.14.0; use strict; use warnings; use File::Basename qw(basename); use File::Copy qw(move); my($infile, @content); my $tab = ' {4}'; if (!$ARGV[0] || !-T $ARGV[0]) { say "Usage: " . basename($0) . " [ text file ]"; exit; } else { $infile = $ARGV[0]; } my $outfile = $infile . '.' . int(rand(10000)); @content = `cat '$infile'`; foreach my $n (0 .. (scalar(@content) - 1)) { my($match) = $content[$n] =~ /^( +)/; # Find lines that start with if ($match) { # spaces. my $write = $match; $write =~ s/($tab)/\t/g; # Translate individual spaces to tabs. $content[$n] =~ s/^$match/$write/; } } open(FILE, '>', $outfile) or die "Can't open '$outfile': $!"; print FILE @content; close(FILE) or die "Can't close '$outfile': $!"; move($outfile, $infile) or die "Can't rename '$outfile': $!";