LinuxQuestions.org
Support LQ: Use code LQCO20 and save 20% on CrossOver Office
Go Back   LinuxQuestions.org > Forums > Linux > Linux - Newbie
User Name
Password
Linux - Newbie This forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Tags used in this thread
Popular LQ Tags , , ,

Reply
 
Thread Tools
Old 01-30-2007, 12:52 AM   #1
donv2
Member
 
Registered: Nov 2004
Location: Upper right corner of USA
Distribution: Ubuntu, unSLUng (NSLU2), Solaris 10 U1 x86 (home), Sun JDS & Solaris SPARC (work)
Posts: 48
Thanked: 0
Question Perl Multi-Line Search & Replace... Can I?...


[Log in to get rid of this advertisement]
Trying to do a search and replace to substitute a separate css stylesheet for a bunch of embedded css in a series of web pages. Essentially I want to do the following:

Replace the following:
Code:
  <style type="text/css">
MANY, MANY MORE LINES OF CSS HERE
THROUGH TO
  /** END CUSTOM SKIN **/
  </style>
With the following:
Code:
<link type="text/css" rel="stylesheet" href="./style.css">
I've read about multi-line selection and thought the following command would do the above, but it didn't. Is it possible to do a multi-line replacement in this fashion? If so, what needs to change to make it work?
Code:
perl -pi -e 's#\<style type\=\"text\/css\"\>.*\<\/style\>#\<link type=\"text\/css\" rel\=\"stylesheet\" href\=\"\.\/style\.css\"\>#igm' *

OR

perl -pi -e 's#\<style type\=\"text\/css\"\>.*\<\/style\>#\<link type=\"text\/css\" rel\=\"stylesheet\" href\=\"\.\/style\.css\"\>#igms' *
TIA,
Don

Last edited by donv2; 01-30-2007 at 01:02 AM..
donv2 is offline  
Tag This Post , , ,
Reply With Quote
Old 01-30-2007, 06:05 AM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938
Thanked: 0
I see three problems.

The first is that the -p option reads your input line by line. It presents each line to your substitution statement, which is not what you want. I think you'll probably have to suck it up and write a real-live Perl script. This script will read the whole file, do the substitution, and write the file out again. (I'm thinking you'll want to do just one substitution per file, but in case you anticipate more than one occurrence of the search string and want to replace them all, don't forget the g switch at the end of the substitution statement.)

It's not difficult to read the whole file in one fell swoop:
Code:
open(INPUT,"<$ARGV[0]") or die;
@input_array=<INPUT>;
close(INPUT);
$input_scalar=join("",@input_array);

# Do your substitution here.

open(OUTPUT,">$ARGV[0]") or die;
print(OUTPUT $input_scalar);
close(OUTPUT);
The second problem is that this won't quite work, because searching ordinarily doesn't do multi-line matching. To fix that, use the * special variable, like this:
Code:
$*=1;
open(INPUT,"<$ARGV[0]") or die;
@input_array=<INPUT>;
close(INPUT);
$input_scalar=join("",@input_array);

# Do your substitution here.

open(OUTPUT,">$ARGV[0]") or die;
print(OUTPUT $input_scalar);
close(OUTPUT);
The third problem is that even this won't work completely, because you want the "." in your search string to match every character; unfortunately, it only matches every character except \n. So in your search string, instead of saying
Code:
.
say
Code:
(.|\n)
Hope this helps.
wjevans_7d1@yahoo.co is offline     Reply With Quote
Old 01-30-2007, 10:59 PM   #3
donv2
Member
 
Registered: Nov 2004
Location: Upper right corner of USA
Distribution: Ubuntu, unSLUng (NSLU2), Solaris 10 U1 x86 (home), Sun JDS & Solaris SPARC (work)
Posts: 48
Thanked: 0

Original Poster
Thumbs up

Thanks! I was successful in using your solution, with the final form like so:
Code:
$*=1;
open(INPUT,"<$ARGV[0]") or die;
@input_array=<INPUT>;
close(INPUT);
$input_scalar=join("",@input_array);
# Do your substitution here.
$input_scalar =~ s#\<style type\=\"text\/css\"\>(.|\n)*\<\/style\>#\<link type=\"text\/css\" rel\=\"stylesheet\" href\=\"\.\/style\.css\"\>#ig;
open(OUTPUT,">$ARGV[0]") or die;
print(OUTPUT $input_scalar);
close(OUTPUT);

Last edited by donv2; 01-30-2007 at 11:10 PM..
donv2 is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Alternative to perl search and replace FirmbIT Programming 2 11-06-2006 09:53 PM
How to "Search & Replace" in html files using Perl? rebel Red Hat 8 04-09-2005 01:58 PM
Multi-file search and replace? miknight Linux - General 4 10-21-2003 07:45 PM
problem in perl replace command with slash (/) in search/replace string ramesh_ps1 Red Hat 4 09-10-2003 02:04 AM
trying to search and replace text file for single & multiple line breaks separately brokenfeet Programming 7 08-29-2003 02:56 PM


All times are GMT -5. The time now is 09:54 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration