Solved

stoopid pattern matching...

Posted on 2000-04-26
5
224 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

803 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