in reply to Re: remove line break from 1 line
in thread remove line break from 1 line

ok, i've switched to line by line... but it's still not removing the line breaks.
#!C:\Perl\bin\perl.exe use strict; use File::Find; use File::Slurp; use Time::Local; print "\nRunning find-slurp_search-within.pl... \n\n"; my $root = "C:/"; # use forward slash, you can use mapped drives. print "changing to $root\n"; chdir $root; my $no_switches=0; my $no_files=0; my @log = (); my $dir; # find (\&Wanted, "department", "managers", "mybranch", "mycity", "myi +nfo", "resources"); find (\&Wanted, "working"); #directories - comman delimited sub Wanted { print "*Processing: $root$File::Find::name \n"; if ($_ =~ /\.htm(l)?$/i) { open(xFILE,$_) or die "ERROR: couldn't open file"; my @file = <xFILE>; my $line; foreach $line (@file) { if ($line =~ m/(<meta[\s\r\n\t]+name="revision"[\s\r\n\t]+con +tent=$)/im) { print $line; chomp($line); close (xFILE); $no_switches++; } # end if } $no_files++; } #end if matches filetype else { print "file type not processed\n\n"; } #add $_ if you want + to see URL of file not processed. } # end sub # add timestamp, # files, # matches to log open(LOG, ">H:/Web/perl/log.txt") or die "ERROR: Can't open log.txt"; my $timestamp = localtime(); print LOG "$no_files files - $no_switches matches - $timestamp\n"; foreach(@log) { print LOG; } close (LOG); print "log file updated. $no_files files - $no_switches matches";

Replies are listed 'Best First'.
Re^3: remove line break from 1 line
by liverpole (Monsignor) on Jan 19, 2007 at 19:02 UTC
    Well, one thing that's immediately obvious is that you are only chomping if the regular expression matches.

    Why not just chomp all lines on input? ...

    chomp(my @file = <xFILE>);

    Another useful idiom allows you to combine my with your foreach statement:

    # No need to do # my $line; # foreach $line (@file) { # foreach my $line (@file) {

    So what do you get when you put a print of the lines after they're input?


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
      Sorry about the last 2 posts... somehow wasn't logged in. I don't want to remove other line breaks, only when the regex matches. EX:
      <meta name="revision" content= "Mon, 05 Jul 2006 23:59:59 GMT">
      I need to get rid of that line break. my output:
      changing to C:/ *Processing: C:/working file type not processed *Processing: C:/working/CIPP_en.html matched: <meta name="revision" content= *Processing: C:/working/Copy of CIPP_en.html matched: <meta name="revision" content= *Processing: C:/working/Copy of index_en.html *Processing: C:/working/index_en.html log file updated. 4 files - 2 matches
      aaah, i know why. It's chomping it in memory... i have to overwrite the file... ugh So, if i'm going line by line, chomp it. Now I need to rewrite the file right? How can i do that? I need the entire file contents to do that. Am I supposed to slurp and read line x line or just do a switch? ---- disregard, i get it now. I have to read it into an array and then write the array
        YEEEHAAA, GOT IT.... any suggestions on tightening it up?
        #!C:\Perl\bin\perl.exe use strict; use File::Find; use File::Slurp; use Time::Local; print "\nRunning ... \n\n"; my $root = "C:/"; # use forward slash, you can use mapped drives. print "changing to $root\n"; chdir $root; my $no_switches=0; my $no_files=0; my @log = (); my $dir; # find (\&Wanted, "department", "managers", "mybranch", "mycity", "myi +nfo", "resources"); find (\&Wanted, "working"); #directories - comman delimited sub Wanted { print "*Processing: $root$File::Find::name \n"; if ($_ =~ /\.htm(l)?$/i) { open(xFILE, $_) or die "ERROR: couldn't open file"; my @file = <xFILE>; foreach my $line (@file) { close (xFILE); if ($line =~ m/(<meta[\s\r\n\t]+name="revision"[\s\r\n\t]+ +content=[\n]+)/i) { open(FILE, ">$_") or die "ERROR: Can't open $_"; chomp($line); print FILE "@file"; # @file array is entire file. close (FILE); print "file overwritten\n"; push @log,"$root$File::Find::name \n\n"; $no_switches++; } # end if } $no_files++; } #end if matches filetype else { print "file type not processed\n\n"; } #add $_ if you want + to see URL of file not processed. } # end sub # add timestamp, # files, # matches to log open(LOG, ">H:/Web/perl/log.txt") or die "ERROR: Can't open log.txt"; my $timestamp = localtime(); print LOG "$no_files files - $no_switches matches - $timestamp\n"; foreach(@log) { print LOG; } close (LOG); print "log file updated. $no_files files - $no_switches matches";