Big problem I need to solve with some unix utils

Joined
Jun 19, 2022
Messages
2
Reaction score
0
I'm the technical guy for a domain that is owned by people who are very creative but have no coding skills at all. I've been doing this on a volunteer basis now for 22 years. A long time ago, I taught the owners about server side includes. This allowed them to create a menu that appears on many of the 3800 pages they have but, if they make a change, they only have to change one file.

Somewhere along the way, the owners bought one of those fancy webpage coding apps and started using it. Apparently, it did not understand server side includes, so it change this code, which works: <!--#include virtual="../../includes/navigate_stack.html" --> to this code, which obviously will never work: <object data="../../includes/navigate_stack.html" type="text/html" width="230px" height="850px">.

There are 1182 pages with this code on them, so obviously I don't want to edit 1182 pages manually. Thus my problem.

I understand I can use sed to replace text on a page, and the beginning of the line is easy:
Code:
sed -i 's/<object data/<!--# include virtual/g' < input_files

The ends of the strings are quite different in the various pages but follow a consistent pattern:

/var/www/html/features/events/bc/jan08/index.html: <object data="../../../../includes/navigate_stack.html" type="text/html" width="230px" height="850px"></object>


/var/www/html/features/events/bc/jan08/index.html: <object data="../../../../includes/ads/5.html" type="text/html" width="230px" height="370px"></object>


/var/www/html/features/events/homecoming2008/index.html: <object data="../../../includes/1.html" type="text/html" width="230px" height="425px"></object>


/var/www/html/features/events/kansascity2007/index.html:<object data="../../../includes/4.html" type="text/html" width="230px" height="250px"></object>


/var/www/html/features/feature_stories/fitzgerald/back/index.html: <object data="../../../../includes/3.html" type="text/html" width="230px" height="350px"></object>

So, would something like this work?

Code:
sed -i 's/type=(some sort of regex to cover the rest)/-->/g'

I don't mind a bit of manual work (like running a shell script in one directory at a time), but I don't want to individually edit 1182 files.
 
Last edited:
Joined
Jan 30, 2023
Messages
107
Reaction score
13
Yes, you can use regular expressions in the sed command to match the end of the string and replace it with the desired value. You can use the following regular expression to match the rest of the string:

CSS:
sed -i 's/\(type="text\/html"\)[^<]*<\/object>/-->/g' input_files

This regular expression matches the text type="text/html" and anything after it until the closing </object> tag. The [^<]* part matches any number of characters (including zero) that are not <. This ensures that the expression matches only up to the closing </object> tag, regardless of what other attributes or values may appear in between.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,931
Messages
2,570,085
Members
46,536
Latest member
keelop

Latest Threads

Top