title: Printing % characters with printf function in perl
body: <p>I have a Perl script that takes as an input a text file containing several sentences (<code>Sentences.txt</code>). Each sentence is separated with a white line. The script creates separate text files for each sentence in <code>Sentences.txt</code>. For example, <code>Sent1.txt</code> for the first sentence in <code>Sentences.txt</code>, <code>Sent2.txt</code> for the second sentence in <code>Sentences.txt</code> and so on.</p>
  
  <p>The problem comes when I try to print a sentence from <code>Sentences.txt</code> to the corresponding separate file (<code>SentX.txt</code>) using the <code>printf</code> function and the sentence contains a <code>%</code> character. How can I solve this?</p>
  
  <p>This is the code:</p>
  
  <pre><code>#!/usr/bin/perl -w
  
  use strict;
  use warnings;
  
  # Separate sentences
  my $sep_dir = "./sep_dir";
  
  # Sentences.txt
  my $sent = "Sentences.txt";
  open my $fsent, "&lt;", $sent or die "can not open '$sent'\n";
  
  # read sentences
  my $kont = 1;
  my $previous2_line = "";
  my $previous_line = "";
  my $mom_line = "";
  while(my $line = &lt;$fsent&gt;){
      chomp($line);
      #
      $previous2_line = $previous_line;
      #
      $previous_line = $mom_line;
      #
      $mom_line = $line;
      if($mom_line !~ m/^\s*$/){
          # create separate sentence file
          my $fitx_esal = "Sent.$kont.txt";
          open my $fesal, "&gt;", $fitx_esal or die "can not open '$fitx_esal'\n";
          printf $fesal $mom_line;
          close $fesal or die "can not close '$fitx_esal'.\n";
          $kont++;
      }
  }
  close $fsent or die "can not close '$sent'.\n";
  </code></pre>