Web Hosting Talk







View Full Version : Help needed with string replacement task


mrzippy
01-21-2004, 07:25 PM
Ack! I've been staring at this problem for way too long. Perhaps there is someone here who knows how to do this! :blush:

The answer can be in perl or a shell.. doesn't matter to me.

I need to basically find all occurences of a certain string pattern, and then replace them with a specific string. But the pattern I need to find is often "hidden" by line breaks, tabs, and various number of spaces.

For example:
Source file data: insert into "energydb".prty_status (audnum,aud_type,audref,
login,date_r,prt_status_code,prt_status_descr,status2,prt_sys_status,status_note1,
status_note2,create_user,update_user,update_datetime) values (0
,'D' ,NULL,USER ,TODAY ,old_prt_stat_rec.prt_status_code ,old_prt_stat_rec.prt_status_descr,
,old_prt_stat_rec.prt_sys_status ,old_prt_stat_rec.status_note1,
old_prt_stat_rec.status_note2 ,old_prt_stat_rec.create_user ,old_prt_stat_rec.update_user
,old_prt_stat_rec.update_datetime ));I need to find all occurences where there is:

1) A trailing comma at the end of the line, and
2) A comma at the start of the next line.

And once found, I need to add a "NULL" string of text after the first comma. So using the source data above, the data should look like this after processing: insert into "energydb".prty_status (audnum,aud_type,audref,
login,date_r,prt_status_code,prt_status_descr,status2,prt_sys_status,status_note1,
status_note2,create_user,update_user,update_datetime) values (0
,'D' ,NULL,USER ,TODAY ,old_prt_stat_rec.prt_status_code ,old_prt_stat_rec.prt_status_descr, NULL
,old_prt_stat_rec.prt_sys_status ,old_prt_stat_rec.status_note1,
old_prt_stat_rec.status_note2 ,old_prt_stat_rec.create_user ,old_prt_stat_rec.update_user
,old_prt_stat_rec.update_datetime ));

Now the problem is that there may be an unknown number of blank spaces, tabs, and line breaks between the two commas.

It doesn't matter to me if the processing is done in shell or perl.

Anyone know how to do this? Please? :D

zealotx
01-21-2004, 09:12 PM
regex

mrzippy
01-21-2004, 09:55 PM
Originally posted by zealotx
regex I've tried using regex, but I can't figure out how to find the "line breaks".

So I can find instances where there are two commas right after each other, but if they are on different lines then I have no idea how to do it.

Thanks!

digitok
01-22-2004, 04:03 AM
This is the PHP version...

$string = preg_replace('/,([\n\t\\s]*),/',',NULL\\\\1,',$string);


Hope this helps.

mrzippy
01-22-2004, 12:11 PM
Originally posted by digitok
This is the PHP version... Unfortunately, we don't have php installed on the system. We only have perl and a few different shells.

Anyone know how to do this in perl?

Is there a "php to perl" translator I can use?

digitok
01-22-2004, 12:46 PM
Well the Regular Expression is there... You just have to find the equivilent replace function in Perl to PHP's preg_replace... preg is based on Perl anyway, so it won't be hard...

Joe Bonanno
01-22-2004, 03:56 PM
Which version of Perl is installed on the system in question?

mrzippy
01-22-2004, 04:03 PM
Hi everyone,

thanks for your help.. I finally was able to get this working:
$ perl -0777 -pi -e 's/,(\s+,)/, NULL$1/g' [filename]