Walt Mankowski on 13 Oct 2012 14:28:29 -0700


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: [PLUG] perl -i question


On Sat, Oct 13, 2012 at 05:15:55PM -0400, JP Vossen wrote:
> >Date: Fri, 12 Oct 2012 19:15:19 -0400
> >From: Walt Mankowski <waltman@pobox.com>
> >
> >On Fri, Oct 12, 2012 at 06:05:28PM -0400, JP Vossen wrote:
> >>See my previous "Perl one-liner to remove duplicates without
> >>changing file order" email for background.
> [...]
> >>Anyone know what I am doing wrong?
> >
> >Yes, the "select(STDOUT) is what's getting you.  Notice that it's
> >outside of the "while (<>)" loop.  Once you fall out of that loop,
> >print goes to STDOUT instead of the file.
> >
> >At a higher level, what's really getting you is that the -i flag has
> >special logic to handle multiple files on the command line.  Whenever
> >if gets to EOF on one file, it closes that one and opens the next one.
> >(That's what's going on when it checks $ARGV.)  Because of that,
> >you've got to print the lines while you've still got the file open.
> >You're circumventing that by gathering them all up printing the lines
> >later.
> 
> OK, that's what I was afraid of, after re-reading the PerlRun stuff
> some more.
> 
> Any ideas how to work-around it in Perl?  Or am I just stuck with
> the usual shell 'cp file file.bak && perl -ne ... file.bak > file'
> method?

That certainly seems like the easiest way to get it to work.

Walt

Attachment: signature.asc
Description: Digital signature

___________________________________________________________________________
Philadelphia Linux Users Group         --        http://www.phillylinux.org
Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce
General Discussion  --   http://lists.phillylinux.org/mailman/listinfo/plug