[Cialug] Text file munging

Zachary Kotlarek zach at kotlarek.com
Thu Jun 18 15:34:14 CDT 2009


On Jun 18, 2009, at 3:19 PM, Dave Weis wrote:

> This does do what I want, any good idea on how I can make it either  
> edit
> in place or spit it out to a different directory with the same name?
>
> I probably should have provided more context in the first request,
> there's a handful of key/value pairs so I can't remove them all.
>
> I guess I can probably for loop it and get this chunk to work if it's
> not easily doable.


Reads from first argument. Writes to second argument. Those must be  
different files or you'll end up with a whole lot of nothing.

You should be able to `find ... -exec clean.pl {} ../output/{} \;` to  
loop through things.

	Zach

#!/usr/bin/perl -w
use strict;

open(IN, $ARGV[0]) or die("Invalid input file\n");
open(OUT, '>', $ARGV[1]) or die("Invalid output file\n");

my $skip_next = 0;
while(<IN>) {
    if (/^\s*\<key\>remote\-id\<\/key\>\s*$/i) {
        $skip_next = 1;
    } elsif ($skip_next && /^\s*\<string\>\d+\<\/string\>\s*$/i) {
        $skip_next = 0;
    } else {
        print OUT $_;
    }
}


close(OUT);
close(IN);



More information about the Cialug mailing list