Solved

closing html tags

Posted on 2004-04-02
4
196 Views
Last Modified: 2010-03-04
I  need a script that closes an open tag...However it must close a specific tag mutiple times.

Below I have the name tag, which needs to be closed through out the document.  Not just three times, but however many times it appears in a document.

<name>

<name>

<story>
story tag text
</story>

<name>



Should look like:


<name>
</name>
<name>
</name>
<story>
story tag text
</story>

<name>
</name>


FYI:
The script below skips the name tag if two appear in a row.  
perl -ape '/^\s*<name>/&&do{$h=1;next;};/^\s*</&&do{if ($h==1) {print "</name>\n";$h=0;}};'
0
Comment
Question by:kennybud416
4 Comments
 
LVL 84

Accepted Solution

by:
ozo earned 25 total points
ID: 10746602
perl -ape '/^\s*</&&do{if ($h==1) {print "</name>\n";$h=0;}};/^\s*<name>/&&do{$h=1;next;};'
0
 
LVL 3

Assisted Solution

by:PerlKing
PerlKing earned 25 total points
ID: 10755473
You assume that there would be only one <name> tag per line.  What if something like the following occurs?

<name> <name>
<name>
<story>
story text
</story>

Does this ever happen? If it does, the following code takes care of it,
perl -ape '/^\s*</ && do { if ($h==1) {print "</name>\n";$h=0;} }; /^\s*<name>/&&do{s/<name>(.*)</<name>$1</name></g; $h=1;next;};'
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

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…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

776 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