title: PERL to Truncate Input Columns Text Files for Output
body: <p>I know there are several examples for truncation in Perl, however for this code objective I have not found a solution in truncating on a text file with 3 columns.</p>
  
  <p>My goal is to use PERL to truncate the 3 columns on a text file to 4 chars only when reading and writing to a text file.</p>
  
  <p>My INPUT text file - input.txt: [Column numbers 1,2,3 and only for reference]</p>
  
  <pre><code>   1                       2                   3
     Rain                  65.22             London
     Snow                  34.44             United States
     Cloudy                23.00             Germany
  </code></pre>
  
  <p>The text file is not tab delimited but only spaces.</p>
  
  <p>My desire OUTPUT file -- output.txt:</p>
  
  <pre><code>1                      2                    3
  Rain                  65.2                  Lond
  Snow                  34.4                  Unit
  Clou                  23.0                  Germ
  </code></pre>
  
  <p>Instead my output.txt displayed:</p>
  
  <pre><code>Rain    Snow    Cloudy
  </code></pre>
  
  <p>Here is my code:</p>
  
  <pre><code>#!/usr/bin/perl
  use strict;
  use warnings;
  
  
  my $input = 'input.txt';
  
  #open file for reading
  open my $fhIn, '&lt;', $input or die qq(Unable to open "$input" for input: $!);
  
  #open file for writing
  my $output = 'output.txt';
  open my $fhOut, '&gt;', $output or die "Can't create output.\n";
  
  
  while( &lt;$fhIn&gt;) {
  
       (s/.{4}\K.*//s);
       print  $fhOut $_;         
  }
  </code></pre>