Home: Perl Programming Help: Regular Expressions:
running regex from command line...a hard one



Lord Xar
Novice

Jan 20, 2004, 4:39 PM


Views: 10732
running regex from command line...a hard one

Hey all,

I have about 500 files thruout different directories and I only want to change the ".html" files. I want to do a open/replace/close sort of regex.

look in the files and any string that looks like this:

[% urlvar %]/home/root/

and replace with this:

[% urlvar %]/home/root/$anewvar

UNLESS:
the original string looks like this:

[% urlvar %]/home/root/common
[% urlvar %]/home/root/browncommon

so, do the replace unless the string has 'these' items.


davorg
Thaumaturge / Moderator

Jan 20, 2004, 11:56 PM


Views: 10727
Re: [Lord Xar] running regex from command line...a hard one

You can do it from the command line:

Code
perl -i.bak -pe 's~\[% urlvar %]/home/root/(?!common|browncommon)~[% urlvar %]/home/root/\$anewvar~g' *.html


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Lord Xar
Novice

Jan 21, 2004, 12:15 PM


Views: 10725
Re: [davorg] running regex from command line...a hard one

thanks..I totally appreciate your help...


Lord Xar
Novice

Jan 21, 2004, 3:18 PM


Views: 10722
Re: [davorg] running regex from command line...a hard one

actually, I have a question.

will this open 'all' files and write to them.
how do you go into directories and subdirectories to make this replace?

I am at the top of the dir. structure....but I need to go into sub folders, and subfolders within to get all the files....


(This post was edited by Lord Xar on Jan 21, 2004, 4:19 PM)


davorg
Thaumaturge / Moderator

Jan 21, 2004, 11:00 PM


Views: 10718
Re: [Lord Xar] running regex from command line...a hard one

This only processes files in one directory. To process all files in a directory tree you have a couple of options:

1/ Use the Unix "find" command from the command line.

2/ Write a Perl program and use the module File::Find.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks