Delete trailing garbage chars from a set of files?

Discussion in 'Perl Misc' started by Patrick Flaherty, Jul 4, 2003.

  1. Hi,

    When I copy a text file from VMS to Windows (via PATHworks) there are often
    (depending on the length of the file) trailing garbage characters. I've
    determined these to be byte value x1a (that it, hex 1a).

    I can delete these from a single file with:

    perl -p0777 -i.bu -e 's/\x1a+$//g' use.lis

    What happens when I copy over a whole bunch of *.lis, many of which might have
    this problem?

    I can loop over all these with:

    @list = glob("*.lis");
    foreach $name (@list) {
    print $name . "\n";
    }

    That prints all the names out. But what I want to do is delete the garbage
    chars from each file in turn.

    This doesn't work (not surprising):

    @list = glob("*.lis");
    foreach $name (@list) {
    s/\x1a+$//g $name;
    }

    Doubtless I'm doing something daft here. Hope someone can help me out.

    pat
     
    Patrick Flaherty, Jul 4, 2003
    #1
    1. Advertising

  2. Patrick Flaherty

    Bart Lateur Guest

    Patrick Flaherty wrote:

    >When I copy a text file from VMS to Windows (via PATHworks) there are often
    >(depending on the length of the file) trailing garbage characters. I've
    >determined these to be byte value x1a (that it, hex 1a).


    I hope those only happen in text files? Because in that case,
    DOS/Windows is capable of ignoring the rest of the file... In fact, in
    perl, when binmode() has not been applied to a filehandle, perl will see
    this as the end of the file.

    Anyway... Can you find out the exact file length on the original system,
    externally? In that case, you could use truncate() to shorten the file.
    See "perldoc -f truncate".

    --
    Bart.
     
    Bart Lateur, Jul 4, 2003
    #2
    1. Advertising

  3. Bernard,

    Haven't done perl in a while, and I did try what you suggest. I believe this
    works on Unix, however *.lis (again, surprisingly) doesn't seem to work on
    Windows.

    pat


    In article <Xns93AE61FDFDF16elhber1lidotechnet@62.89.127.66>, "Bernard says...
    >
    >Patrick Flaherty wrote:
    >
    >> Hi,
    >>
    >> When I copy a text file from VMS to Windows (via PATHworks) there are
    >> often (depending on the length of the file) trailing garbage
    >> characters. I've determined these to be byte value x1a (that it, hex
    >> 1a).
    >>
    >> I can delete these from a single file with:
    >>
    >> perl -p0777 -i.bu -e 's/\x1a+$//g' use.lis
    >>
    >> What happens when I copy over a whole bunch of *.lis, many of which
    >> might have this problem?

    >
    >
    >[...]
    >
    >
    >> Doubtless I'm doing something daft here. Hope someone can help me
    >> out.

    >
    >
    >Just change:
    >
    >
    > perl -p0777 -i.bu -e 's/\x1a+$//g' use.lis
    >
    >
    >into:
    >
    >
    > perl -p0777 -i.bu -e 's/\x1a+$//g' *.lis
    >
    >
    >--
    >Cheers,
    >Bernard
    >--
    >echo 42|perl -pe '$#="Just another Perl hacker,"'
    >
     
    Patrick Flaherty, Jul 4, 2003
    #3
  4. Thanx to everyone that replied.

    This is what worked for me. Like a charm. Thanx Tad.

    Was I just hallucinating (I don't think so) or why does feeding *.lis to a
    cmd-line invocation of perl _on Windows_ not work?

    pat



    In article <>,
    says...
    >
    >Patrick Flaherty <> wrote:
    >
    >> perl -p0777 -i.bu -e 's/\x1a+$//g' use.lis
    >>
    >>What happens when I copy over a whole bunch of *.lis, many of which might have
    >> this problem?

    >
    >
    >> This doesn't work (not surprising):
    >>
    >> @list = glob("*.lis");
    >> foreach $name (@list) {
    >> s/\x1a+$//g $name;
    >> }

    >
    >
    >But this works (surprisingly!):
    >
    > # untested
    > local $^I = '.bu'; # enable inplace editing
    > local @ARGV = glob '*.lis'; # load up the filenames
    > while ( <> ) {
    > s/\x1a+$//g;
    > print;
    > }
    >
    >
    >In-place editing works on whatever is in @ARGV. It does not care
    >how the stuff got into @ARGV, it will just be expecting that they
    >are all names of files to edit.
    >
    >
    >--
    > Tad McClellan SGML consulting
    > Perl programming
    > Fort Worth, Texas
     
    Patrick Flaherty, Jul 4, 2003
    #4
  5. Patrick Flaherty <> wrote:


    > works on Unix, however *.lis (again, surprisingly) doesn't seem to work on
    > Windows.



    Being disappointed by Windows is not at all surprising. :-(


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jul 4, 2003
    #5
  6. Patrick Flaherty <> wrote:

    > This is what worked for me. Like a charm. Thanx Tad.



    You can pay me back by learning to quote your followups properly.

    Please do not post in backwards-time fashion.



    [ snip TOFU ]

    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jul 4, 2003
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave
    Replies:
    0
    Views:
    370
  2. Kosio

    Floats to chars and chars to floats

    Kosio, Sep 16, 2005, in forum: C Programming
    Replies:
    44
    Views:
    1,309
    Tim Rentsch
    Sep 23, 2005
  3. Konstantinos Pachopoulos

    trim start and trailing space chars from string

    Konstantinos Pachopoulos, Sep 15, 2007, in forum: Python
    Replies:
    1
    Views:
    460
    John Machin
    Sep 15, 2007
  4. Hongyu
    Replies:
    9
    Views:
    930
    James Kanze
    Aug 8, 2008
  5. björn lundin

    responseXML has trailing garbage

    björn lundin, Sep 24, 2011, in forum: Javascript
    Replies:
    2
    Views:
    902
    björn lundin
    Sep 25, 2011
Loading...

Share This Page