[PLUG] Perl Question

M. Edward (Ed) Borasky znmeb at cesmail.net
Tue Jul 26 05:04:21 UTC 2005


Doug Davis wrote:

> I know there are a lot of Perl experts here, so I thought I would ask 
> about a problem I have.
>
> I have a very large file that is exported from an application. Its all 
> one line and more than 3000 characters so sed/awk bails out on me, I 
> suggested Perl might be a good choice, but we are having problems with 
> the regex matching. There are no delimiters, but each field starts 
> with a constant prefix and is a fixed length.
>
> What I want to do in English is to find this string plus the next 80 
> characters (combination of text and whitespace, and replace it with 
> the same string + a replacement string + fill the remaining spaces 
> with whitespace. What's the best way to do this? Just to give you a 
> point of reference, we are replacing actual customer names with 
> something like John Doe.

Ah ... yes. Something like

$hugestring =~ s/NAME=.{80}/NAME=${newname}/g;

where "$newname" contains "John Doe" followed by 72 spaces, etc.

The context indicates you've been given some kind of database dump, 
sadly not the traditional comma-separated or tab-separated value format 
with text strings enclosed in quotes and terminated by newlines. Is it 
possible to get the application to export in such a format, which would 
make this a rather simple loop?

I'm also guessing you don't want to replace every person's name with 
"John Doe" but a different name for each original name. So rather than 
use a one-shot replace command, you somehow might need to break the 
string up into individual records, replace each name field with a random 
one, then reassemble the string. Is that correct? If so, is there any 
kind of "record separator" in your input string?

Finally, do you need to re-import the resulting string back into the 
application?




More information about the PLUG mailing list