JP Vossen on 12 Oct 2012 15:05:33 -0700


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

[PLUG] perl -i question


See my previous "Perl one-liner to remove duplicates without changing file order" email for background.

I can understand why this might not work (note: -i'.bak'):
$ perl -ni'.bak' -e '$line{$_} = $.; END { for (sort{$line{$a}<=>$line{$b}} keys %line) {print} }' /tmp/sample.hist

I'm *guessing* the END{} block executes *after* all the file handles are closed, so you end up writing nothing to your file. (Hope you have a backup.)

But why doesn't this work:
$ perl -i'.bak' -e 'while (<>) {$line{$_} = $.} for (sort{$line{$a}<=>$line{$b}} keys %line) {print}' /tmp/sample.hist

These *do* work:
	$ perl -ni'.bak' -e '$line{$_}++ or print' /tmp/sample.hist
	$ perl -i'.bak' -e 'while (<>) { $line{$_}++ or print }' /tmp/sample.hist

But these do not either:
$ perl -ni'.bak' -e '$line{$_}++; END { for (sort{$line{$a}<=>$line{$b}} keys %line) {print} }' /tmp/sample.hist $ perl -i'.bak' -e 'while (<>) {$line{$_}++} for (sort{$line{$a}<=>$line{$b}} keys %line) {print}' /tmp/sample.hist

So WTH??? I've Googled the heck out and this and read http://perldoc.perl.org/perlrun.html#*-i*[_extension_] 10 times, but nothing I try works, print always goes to STDOUT and my file always ends up zero bytes. I'm guessing the "select(STDOUT);" in line 21 of the listing in the URL is what is getting me, but I can't figure out how to work around it in Perl. (I can easily work around this outside of Perl, I just don't wanna.) I've tried all kinds of crazy stuff like: $ perl -i'.bak' -e 'while (<>) {$line{$_} = $.;} select(ARGV); for (sort{$line{$a}<=>$line{$b}} keys %line) {print}' /tmp/sample.hist $ perl -i'.bak' -e 'while (<>) {$line{$_} = $.;} select(ARGVOUT); for (sort{$line{$a}<=>$line{$b}} keys %line) {print}' /tmp/sample.hist $ perl -i'.bak' -e 'while (<>) {$line{$_} = $.;} for (sort{$line{$a}<=>$line{$b}} keys %line) {print ARGV}' /tmp/sample.hist $ perl -i'.bak' -e 'while (<>) {$line{$_} = $.;} for (sort{$line{$a}<=>$line{$b}} keys %line) {print ARGVOUT}' /tmp/sample.hist

And anything else I can think of, but it all silently fails (and nukes my file) on both Debian Perl 5.10.1 and XP ActiveState (810) 5.8.4, so I'm pretty sure Perl is WaD and I'm just missing something obvious.

Anyone know what I am doing wrong?

Thanks,
JP
----------------------------|:::======|-------------------------------
JP Vossen, CISSP            |:::======|      http://bashcookbook.com/
My Account, My Opinions     |=========|      http://www.jpsdomain.org/
----------------------------|=========|-------------------------------
"Microsoft Tax" = the additional hardware & yearly fees for the add-on
software required to protect Windows from its own poorly designed and
implemented self, while the overhead incidentally flattens Moore's Law.
___________________________________________________________________________
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