Loading ...
Sorry, an error occurred while loading the content.

Re: Appending a period to end of line

Expand Messages
  • Damien Carbery
    ... If you are on Unix, working with files from Windows (with ^M at the end of each line, as you d see with vi ), I don t think chomp will remove the ^M
    Message 1 of 2 , Sep 30, 2003
    View Source
    • 0 Attachment
      --- In perl-beginner@yahoogroups.com, "Nathan E" <nathan@o...> wrote:
      > Hey all;
      >
      > I'm having a problem. It's a SIMPLE problem but for some reason my
      > code just isn't co-operating. I have a set of text files (all in the
      > same directory that consists of only those files I want to process).
      > These files are basically sentences that are delimited by a new line so;
      >
      > sentence is here
      > and this is another sentence
      > yet another sentence can be found here
      >
      > I need to append a period to the end of each of these sentences. The
      > code I tried to use is pasted below. The only problem is that the
      > output ends up being;
      >
      > sentence is here
      > .
      > and this is another sentence
      > .
      > yet another sentence can be found here
      > .
      >
      > Can anyone help?
      >
      > ----------------------------------
      >
      > print "Input Directory: ";
      > $indir = <stdin>;
      > chomp $indir;
      >
      > print "Output Directory: ";
      > $outdir = <stdin>;
      > chomp $outdir;
      >
      > opendir(DIR, $indir) or die "\nCould not open directory\n";
      > @files = readdir(DIR);
      > splice @files, 0, 2;
      >
      > foreach $file (@files) {
      > $infilename = $indir."/".$file;
      > $outfilename = $outdir."/".$file;
      >
      > open(IN, "<$infilename") or die "\nCould not open file: $infilename\n";
      > open(OUT, ">$outfilename") or die "\nCould not write file:
      > $outfilename\n";
      >
      > while ( <IN> ) {
      > chomp $_;
      > print OUT "$_\.\n";
      > }
      > close(OUT);
      > close(IN);
      > }
      >
      > ----------------------------------

      If you are on Unix, working with files from Windows (with ^M at the
      end of each line, as you'd see with 'vi'), I don't think chomp will
      remove the ^M chars.

      In a shell you could do:
      for f in *
      do
      sed 's/$/./' $f /outdir/$f
      done
    Your message has been successfully submitted and would be delivered to recipients shortly.