Dear Monks,

I am struggling with text files, which I want to modify with regex. I converted a pdf file to a text file which then contains the following text:

"Inventories

Trade receivables

Assets

This item includes impairment losses."

I now want to remove all lines which are not ending with a period (lines Inventories, Trade receivables, Assets) and have the following code:

use strict; use warnings; use File::Copy; my $destination = "/directory/originalinputfiles/"; my @files = glob ("/directoy/*.txt"); foreach my $file (@files) { my $newfile = $file.".tmp"; open (IN,'<:encoding(UTF-8)', $file) or die "Could not open '$file' +$!"; open (OUT,'>:encoding(UTF-8)', $newfile) or die "Could not open '$ne +wfile' $!"; while (my $text = <IN>) { $text =~ s/^[a-z].*[a-z]$//gmi; print OUT $text; } close (IN) or die "Could not close input file: '$file' $!"; close (OUT) or die "Could not close output file '$newfile' $!"; move $file, $destination or die "Could move the orignal input file: +$file to the folder: '$destination' $!"; rename $newfile, $file or die "Could not rename file: '$newfile' $!" +; print "\n done \n"; }

If I run this script the first three lines are not identified and removed (Inventories, Trade receivables, Assets). However, adding a paragraph manually in the text file after every new line leads to the identification and removal of these lines (Inventories, Trade receivables, Assets) (which is not an option as I have thousands of text files).

I already tried to add a line after every line with regex, which also does not work

 $text =~ s/$/\n/;

Why is my code not working and how can I solve it?


In reply to Perl regex txt file new line (not recognised?) by thurinus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.