CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



Lord Xar
Novice

Jan 20, 2004, 4:39 PM

Post #1 of 5 (3594 views)
running regex from command line...a hard one Can't Post

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

Post #2 of 5 (3589 views)
Re: [Lord Xar] running regex from command line...a hard one [In reply to] Can't Post

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

Post #3 of 5 (3587 views)
Re: [davorg] running regex from command line...a hard one [In reply to] Can't Post

thanks..I totally appreciate your help...


Lord Xar
Novice

Jan 21, 2004, 3:18 PM

Post #4 of 5 (3584 views)
Re: [davorg] running regex from command line...a hard one [In reply to] Can't Post

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

Post #5 of 5 (3580 views)
Re: [Lord Xar] running regex from command line...a hard one [In reply to] Can't Post

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

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives