Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

File end marker problem

by DaveKelly (Initiate)
on Apr 01, 2003 at 11:46 UTC ( [id://247214]=perlquestion: print w/replies, xml ) Need Help??

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");

while(<FILE>){
s/\x1a//;
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 (Patriarch) 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).

    CU
    Robartes-

      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;

    --Coplan

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://247214]
Approved by robartes
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-03-19 08:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found