[Cialug] Text file munging
Dave Weis
djweis at internetsolver.com
Thu Jun 18 15:19:35 CDT 2009
Zachary Kotlarek wrote:
> On Jun 18, 2009, at 12:45 PM, Dave Weis wrote:
>> I have a bunch of individual email messages from mail.app that have been
>> disassociated from their mailbox. I have tried copying them into the
>> messages folder but they don't go because of two lines in the plist at
>> the
>> end. I need to remove
>> <key>remote-id</key>
>> <string>4294</string>
>> from 20,000 files. The number varies and the location at the end of the
>> file can move a few lines at a time. I've tried my regex magic on it but
>> don't have enough to get it working. Any help?
>
> This works (at least on my .emlx files), and doesn't drop other string
> values you might need from the plist.
>
> It does not do anything to protect plists in your message bodies though.
> I couldn't figure out how Apple was delimiting the plist at the end, or
> escaping plists in the body, and I was too lazy to read backwards from
> the end to find the last <plist> line.
>
> Reads STDIN, writes STDOUT
>
> --
>
> #!/usr/bin/perl -w
> use strict;
>
> my $skip_next = 0;
> while(<>) {
> 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 $_;
> }
> }
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.
Thanks!!
dave
--
Dave Weis
515-224-9229
djweis at internetsolver.com
http://www.internetsolver.com/
More information about the Cialug
mailing list