DaveKelly has asked for the wisdom of the Perl Monks concerning the following question:

I have a single file that is a concatination of a lot of other files. Each single file contains a file end marker (\x1a) and therefore the concat file contains multiple file end markers. Please tell me there is a simple way to loop through the concat file a remove all the file end markers. I tried this ....
#!/usr/bin/perl -w

binmode FILE;
open (FILE, "<concatfile");
open (OUT, ">x.txt");

print OUT;
But it stops after the first file end marker. Any ideas?

Replies are listed 'Best First'.
Re: File end marker problem
by BrowserUk (Pope) on Apr 01, 2003 at 12:15 UTC

    If this is on a DOSish system (which doesn't seem likely given the shebang line your using?) then you would need to move the binmode statement to after the open to which it applies for it to have any effect.

    If your on a *nix system, then you shouldn't need the binmode (although I did see something about this changing under some circumstances with 5.8? -- Warning:Vague recollection), but if that is the case, then you probably wouldn't be seeing the problem.

    Examine what is said, not who speaks.
    1) When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
    2) The only way of discovering the limits of the possible is to venture a little way past them into the impossible
    3) Any sufficiently advanced technology is indistinguishable from magic.
    Arthur C. Clarke.
      Super - thats got it licked. Thanks a lot. Dave
Re: File end marker problem
by robartes (Priest) on Apr 01, 2003 at 12:05 UTC
    Try this:
    perl -ne 'print unless /\x1a/;' concatfile >x.txt

    Update: This only works if the \x1a is on a line by itself. If not, try this:

    perl -npe 's/\x1a//;' concatfile >x.txt
    This is however virtually the same thing as yours. It works for me on a file with random \x1a scattered throughout it (as does your code if I drop the binmode. If you're on Unix, you don't need it, and you would need to put it after the open of concatfile for it to work as well).


      He did not say the marker is on a separate line.
Re: File end marker problem
by Coplan (Pilgrim) on Apr 01, 2003 at 17:59 UTC
    What I have done in the past is to store things into a variable before printing the output. If the file isn't drastically huge, this won't be a problem:
    # Just a snippet -- assume variables decared, etc. while(<FILE>) { $out =~ s/\x1a//; } print $out;