in reply to Perl regex txt file new line (not recognised?)

> I now want to remove all lines which are not ending with a period

this works for me

use strict; use warnings; while (<DATA>) { print if /\.$/; } __DATA__ Inventories Trade receivables Assets This item includes impairment losses.

-->

This item includes impairment losses.

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Replies are listed 'Best First'.
Re^2: Perl regex txt file new line (not recognised?)
by thurinus (Initiate) on Jan 21, 2020 at 15:52 UTC

    Dear LanX,

    thanks for the fast reply. With this code as well the lines (Inventories, Trade receiveables, Assets) in my text file are not recognized. Is there something wrong with the textfile and how can I change it?

      maybe try also a chomp $text in your code.

      update

      here several methods to clarify what your file contains.

      I prefer Data::Dump but you might not have it installed.

      use strict; use warnings; use Data::Dump qw/dd/; use Data::Dumper; while (<DATA>) { chomp; print "<$_>\n" if /\.$/; dd $_; warn Dumper $_; } __DATA__ Inventories Trade receivables Assets This item includes impairment losses.

      with chomp

      $VAR1 = 'Inventories'; $VAR1 = ''; $VAR1 = 'Trade receivables'; $VAR1 = ''; $VAR1 = 'Assets'; $VAR1 = ''; $VAR1 = 'This item includes impairment losses.'; "Inventories" "" "Trade receivables" "" "Assets" "" <This item includes impairment losses.> "This item includes impairment losses."

      without chomp

      $VAR1 = 'Inventories '; $VAR1 = ' '; $VAR1 = 'Trade receivables '; $VAR1 = ' '; $VAR1 = 'Assets '; $VAR1 = ' '; $VAR1 = 'This item includes impairment losses. '; "Inventories\n" "\n" "Trade receivables\n" "\n" "Assets\n" "\n" <This item includes impairment losses. > "This item includes impairment losses.\n"

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      > the lines (Inventories, Trade receiveables, Assets) in my text file are not recognized.
      • What do you mean "not recognized", are they are printed?
      • What happens if you run my code?
      • What happens if you copy my DATA lines into a file?

      My guess is that your input is different to what you think.

      Do you probably have weird line endings?

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

      Even if your disc file is exactly what you expect, you must fix your regex to match and remove the newlines from the unwanted lines.
      use strict; use warnings; my $file = \do{my $disk_file = "Inventorier\n" ."Trade receivables\n" ."Assets\n" ."This item includes impairment losses.\n" ; }; open (IN,'<:encoding(UTF-8)', $file) or die "Could not open '$file'$!" +; *OUT = *stdout; while (my $text = <IN>) { #$text =~ s/^[a-z].*[a-z]$//gmi; $text =~ s/^[a-z].*[a-z]\n$//i; print OUT $text; } close (IN) or die "Could not close input file: '$file' $!";

      Note that I have not made any other changes to your posted code (except to remove the looping and the output files.) Even though this should 'work', I prefer LanX's first suggestion.

      Bill