Solved

stoopid pattern matching...

Posted on 2000-04-26
5
212 Views
Last Modified: 2010-03-05
I've got the following text in a UNIX text file that I need to do a pattern match on:

<SCRIPT LANGUAGE="JavaScript">
    pagetitle="GLCC Home Page";
</SCRIPT>

I'm trying to do a find-and-replace through the command line using this syntax:
    perl -pi.bak -e 's#...#...#oi'  *.html
But I can only match over one line. That is, I can match 'Script">\n' and I can match '\s+pagetitle', but I can't match 'Script">\n\s+pagetitle' for some obnoxious reason. My Camel book hasn't been enlightening. Help?
0
Comment
Question by:mblase
  • 2
  • 2
5 Comments
 
LVL 16

Expert Comment

by:maneshr
ID: 2753009
try this..

perl -pi.bak -e 'BEGIN{$/="";} s#Script">\s+pagetitl#SCR#oi' *.html
0
 
LVL 2

Author Comment

by:mblase
ID: 2753127
Thanks. But since this is a 100-pointer, I won't award points until you tell me just why the heck that works. :-)
0
 
LVL 16

Accepted Solution

by:
maneshr earned 100 total points
ID: 2753163
The problem is the -p switch

this switch causes Perl to assume the following loop around your script, which makes it iterate over filename arguments rather as sed does:

     LINE:
     while (<>) {
         ...                # your script goes here
     } continue {
         print;
     }

But you wanted the entire file to be treated as one single line.
in order to do that you have to make the $/ (INPUT RECORD SEPARATOR)variable to ignore \n as the line seperator.
We do that using $/="";

Unfortunately, that cant be achieved by putting the $/=""; within the while(<>) loop but by using the BEGIN sub routine.

A BEGIN subroutine is executed as soon as possible, that is, the moment it is completely defined, even before the rest of the containing file is parsed.

This causes the while loop to read the entire input, .html, file as a single line!!

let me know if you are still unclear and i will make it more simpler.
0
 
LVL 2

Author Comment

by:mblase
ID: 2753188
ahhhhhhhh.... thanks for the explanation of the -p switch. I was wondering why all the //s and //m switches wouldn't work, and the BEGIN{} was necessary. This'll come in way handy down the road, too. Many thanks!
0
 
LVL 84

Expert Comment

by:ozo
ID: 2753407
perl -0777 -pi.bak -e 's#...#...#oi'  *.html
perldoc perlrun
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now