Hi Barry, I made a slightly bigger test file from your input for testing. ===================================================================== <table class="socialheader" width="100%" border="0"> <tr> <td width="85%" align="left" nowrap>The Three Sisters, Glen Coe</td> </tr> </table> <table class="sociheader" width="100%" border="0"> <tr> <td width="85%" align="left" nowrap>This cannot be used</td> </tr> </table> <table class="socialheader" width="100%" border="0"> <tr> <td width="85%" align="left" nowrap>This is different each time</td> </tr> </table> <table class="socialhear" width="100%" border="0"> <tr> <td width="85%" align="left" nowrap>This is not usable</td> </tr> </table> ========================================================================= I think the perl script below (or some modification thereof) will do what you want. ===================================================== #! /usr/bin/perl use strict; open(INFILE, "<$ARGV[0]"); my $social_flag = 0; while(<INFILE>) { my $line=$_; chomp($line); if($line=~m/\<table class\=\"socialheader\"/) { $social_flag = 1; print("$line\n"); next; } if($line=~m/align\=\"left\"/ and $line=~m/width\=\"85\%\"/ and $line=~m/nowrap/ ) { if($social_flag) { print("$line\n"); print("One new line here\n"); print("Another new line here\n"); $social_flag = 0; } else { print("$line\n"); } } else { print("$line\n"); } } close(INFILE); ============================================================== Hope this helps. Govind
Subject: [ALUG] Search and replace(insert) - can anyone help? To: Alug <main@lists.alug.org.uk> Message-ID: <1288625418.1815.0@dataman1> Content-Type: text/plain; charset=us-ascii
<table class="socialheader" width="100%" border="0">
<tr> <td width="85%" align="left" nowrap>The Three Sisters, Glen Coe</td> </tr>
</table>
I have over 2500 pages with the above HTML in ( the "Three Sisters, Glen Coe" bit is different in every file) and I want to add two lines so:
<table class="socialheader" width="100%" border="0">
<tr> <td width="85%" align="left" nowrap>The Three Sisters, Glen Coe</td> <One new line here> <Another new line here> </tr>
</table>
The pair of lines to be inserted is identical in each case but the only unique identifier is the "class=socialheader".
Is there any way I can do it with a script?
-- Barry Samuels http://www.beenthere-donethat.org.uk The Unofficial Guide to Great Britain
Govind It was very kind of you to take the trouble to write that script and, although my Perl is not especially good, I should be able to make a few modifications so that it will go through all files in a specified directory. So, yes, it does help. Thank you very much. -- Barry Samuels http://www.beenthere-donethat.org.uk The Unofficial Guide to Great Britain On 02/11/10 19:00:27, Govind Chandra wrote:
Hi Barry,
I think the perl script below (or some modification thereof) will do what you want.
=====================================================
#! /usr/bin/perl use strict;
open(INFILE, "<$ARGV[0]");
my $social_flag = 0;
while(<INFILE>) { my $line=$_; chomp($line);
if($line=~m/\<table class\=\"socialheader\"/) { $social_flag = 1; print("$line\n"); next; }
if($line=~m/align\=\"left\"/ and $line=~m/width\=\"85\%\"/ and $line=~m/nowrap/ ) { if($social_flag) { print("$line\n"); print("One new line here\n"); print("Another new line here\n"); $social_flag = 0; } else { print("$line\n"); } } else { print("$line\n"); }
}
close(INFILE);
==============================================================
Hope this helps.
Govind
Subject: [ALUG] Search and replace(insert) - can anyone help? To: Alug <main@lists.alug.org.uk> Message-ID: <1288625418.1815.0@dataman1> Content-Type: text/plain; charset=us-ascii
<table class="socialheader" width="100%" border="0">
<tr> <td width="85%" align="left" nowrap>The Three Sisters, Glen Coe</td> </tr>
</table>
I have over 2500 pages with the above HTML in ( the "Three Sisters, Glen Coe" bit is different in every file) and I want to add two lines so:
<table class="socialheader" width="100%" border="0">
<tr> <td width="85%" align="left" nowrap>The Three Sisters, Glen Coe</td> <One new line here> <Another new line here> </tr>
</table>
The pair of lines to be inserted is identical in each case but the only
unique identifier is the "class=socialheader".
Is there any way I can do it with a script?
-- Barry Samuels http://www.beenthere-donethat.org.uk The Unofficial Guide to Great Britain
_______________________________________________ main@lists.alug.org.uk http://www.alug.org.uk/ http://lists.alug.org.uk/mailman/listinfo/main Unsubscribe? See message headers or the web site above!
On 03/11/10 14:44, Barry Samuels wrote:
Govind
It was very kind of you to take the trouble to write that script and, although my Perl is not especially good, I should be able to make a few modifications so that it will go through all files in a specified directory.
So, yes, it does help. Thank you very much.
It's a bit late, and a bit moot now as you have a script to work with, but I think you may be able to also do it with SED or AWK. As for modifying the script, as it accepts a filename parameter, you should just be able to call it from the commandline using FOR and Bob's your Mother's Brother.... This might do it - I'm not an expert on the syntax of for. Note I think they're "Back-ticks" ` (key to left of the 1 key), not apostrophes ' for filename in `ls *.html`; do run_my_script $filename; done HTH Steve
participants (3)
-
Barry Samuels -
Govind Chandra -
steve-ALUG@hst.me.uk