Fedora Linux Support Community & Resources Center
  #1  
Old 28th March 2011, 09:01 AM
lothario Offline
Registered User
 
Join Date: Jun 2004
Posts: 130
linuxchrome
conditionally replace an HTML tag in a large file

We receive this large file which is generated as a HTML page.
So anybody can view it with a web brower.
A few lines from this file ...



Code:
<tr><td><font color="#555555" size="3" face="Tahoma, Arial, Verdana, Helvetica">  1999&nbsp; <font color="#cc6600" size="5">&larr;</font> &nbsp;<font color="#0066ff" size="4">03</font>-27 23:36:50 &rarr; <font color="#9932cc" size="2">left</font> &nbsp; &rarr; &nbsp; <font color="#800000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Stub:</font> <font color="#0066ff" size="2">star</font><font color="#bb6633" size="2"> start</font> Block to negative <font color="#228b22" size="3">&nbsp; Orange </font>        </font></td></tr>  

<tr><td><font color="#555555" size="3" face="Tahoma, Arial, Verdana, Helvetica">  1998&nbsp; <font color="#cc6600" size="5">&larr;</font> &nbsp;<font color="#0066ff" size="4">03</font>-27 23:36:48 &rarr; <font color="#9932cc" size="2">right</font> &nbsp; &rarr; &nbsp; <font color="#800000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Stub:</font> <font color="#0066ff" size="2">star</font><font color="#9933cc" size="2"> return</font> from negative <font color="#228b22" size="3">&nbsp; Green </font>        </font></td></tr>  

<tr><td><font color="#555555" size="3" face="Tahoma, Arial, Verdana, Helvetica">  1997&nbsp; <font color="#cc6600" size="5">&larr;</font> &nbsp;<font color="#0066ff" size="4">03</font>-27 23:36:46 &rarr; <font color="#9932cc" size="2">up</font> &nbsp; &rarr; &nbsp; <font color="#800000">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Stub:</font> <font color="#0066ff" size="2">star</font><font color="#9933cc" size="2"> return</font> from negative <font color="#228b22" size="3">&nbsp; Yellow </font>        </font></td></tr>
Note:
Each <tr><td> ... </td></tr> is on a single line.
This file has hundreds of lines.

We need to make a small change to this file so it is easier to read.
If the <tr><td> ... </td></tr> line has the word "Yellow" then
we want to replace the '<td>' tag
with '<td bgcolor="#FFFF00">'
so that the line with with word "Yellow" has a yellow background.

How can I do this with a bash shell script?
I can add the shell script to the cron job that delivers this HTML file.
Reply With Quote
  #2  
Old 28th March 2011, 09:29 AM
japafi Offline
Registered User
 
Join Date: Mar 2010
Posts: 87
linuxfirefox
Re: conditionally replace an HTML tag in a large file

Search with your favorite search engine for perl, search & replace, backreferences.

Quick (and most likely not working) suggestion would be something like

perl -pi -e 's/<tr><td>(.*)Yellow/<tr><td bgcolor="#FFFF00">$1/g' [inputfile.ext]
Reply With Quote
  #3  
Old 28th March 2011, 10:03 AM
giulix Offline
"Fixed" by (vague) request
 
Join Date: Oct 2005
Location: GMT+ 1
Posts: 2,950
linuxfedorafirefox
Re: conditionally replace an HTML tag in a large file

Code:
awk '/Yellow/ {sub(/<td>/, "<td bgcolor=\"#FFFF00\">")} {print}' oldfile.html >newfile.html
Reply With Quote
  #4  
Old 28th March 2011, 11:18 AM
lothario Offline
Registered User
 
Join Date: Jun 2004
Posts: 130
linuxchrome
Re: conditionally replace an HTML tag in a large file

Quote:
Originally Posted by giulix View Post
Code:
awk '/Yellow/ {sub(/<td>/, "<td bgcolor=\"#FFFF00\">")} {print}' oldfile.html >newfile.html
Thanks, that worked perfectly.
Reply With Quote
  #5  
Old 28th March 2011, 02:47 PM
briand Offline
Registered User
 
Join Date: Nov 2010
Location: Tallahassee, FL
Posts: 132
windows_xp_2003ie
Re: conditionally replace an HTML tag in a large file

of course it did!

just remember: if awk is the "swiss army knife" of text processing, then perl is the "swiss army chainsaw".
Reply With Quote
  #6  
Old 28th March 2011, 02:57 PM
giulix Offline
"Fixed" by (vague) request
 
Join Date: Oct 2005
Location: GMT+ 1
Posts: 2,950
linuxfedorafirefox
Re: conditionally replace an HTML tag in a large file

This must be the Swiss safety-pin, then:
Code:
sed -i '/Yellow/ s/<td>/<td bgcolor="#FFFF00">/' file.html
It overwrites the input file, though...
Reply With Quote
  #7  
Old 28th March 2011, 03:00 PM
jpollard Offline
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,777
linuxfedorafirefox
Re: conditionally replace an HTML tag in a large file

Just remember that the pattern /Yellow/ will replace EVERY occurrence of the word Yellow, even when it does NOT occur in a "<font color=..." tag.
Reply With Quote
  #8  
Old 28th March 2011, 03:09 PM
giulix Offline
"Fixed" by (vague) request
 
Join Date: Oct 2005
Location: GMT+ 1
Posts: 2,950
linuxfedorafirefox
Re: conditionally replace an HTML tag in a large file

Actually, it applies the substitution to all lines that contain the word Yellow. It doesn't touch "Yellow" at all
Reply With Quote
  #9  
Old 28th March 2011, 03:18 PM
jpollard Offline
Registered User
 
Join Date: Aug 2009
Location: Waldorf, Maryland
Posts: 6,777
linuxfedorafirefox
Re: conditionally replace an HTML tag in a large file

Dang.. missed that it didn't use any fields...
Reply With Quote
Reply

Tags
conditionally, file, html, large, replace, tag

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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 On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to have an RPM replace another in the SPEC file? uncholowapo Using Fedora 0 30th September 2009 05:48 AM
Not sure why my index2.php file is showing instead of my index.html file billperrotta Servers & Networking 9 12th December 2008 03:41 AM
'File too large' when copying 6.3GB file from ext3 to USB arun_maurya Using Fedora 4 8th December 2008 11:24 AM
Moving a part of an HTML file to another file panpol Programming & Packaging 5 28th October 2005 09:03 AM
fixing up the bug-need to replace a file ches Using Fedora 3 11th July 2005 06:42 PM


Current GMT-time: 08:59 (Friday, 25-07-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat