how to delete certain java script from html files using sed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to delete certain java script from html files using sed
# 1  
Old 04-25-2012
how to delete certain java script from html files using sed

I am cleaning forum posts to convert them in offline reading version with clean html text. All files are with html extension and reside in one folder. There is some java script i would like to remove, which looks like
Code:
<script LANGUAGE="JavaScript1.1">
<!--

function mMz()
{
 var mPz = "";
 for(var prop in this) {
 if ((prop.charAt(0) == '_' && prop.charAt(prop.length-1)=='_')
		      || ((typeof this[prop]) == 'function')) 
 continue;
 if (mPz != "") mPz += '&';
 mPz += prop + ':' + escape(this[prop]);
 }
 var cookie = this.gHa + '=' + mPz;
 if (this._expiration_)
 cookie += '; expires=' + this._expiration_.toGMTString();
 if (this._path_) cookie += '; path=' + this._path_;
 if (this._domain_) cookie += '; domain=' + this._domain_;
 if (this._secure_) cookie += '; secure';
 
 this._document_.cookie = cookie;
}
//-->
</script>

I tried using following sed
command

Code:
sed -i.bak '/\<script LANGUAGE="JavaScript\1\.\1"\>/,/<\/script\>/d' *.html

but it results in error

Code:
sed: -e expression #1, char 40: Invalid back reference

Any help will be greatly appreciated
# 2  
Old 04-25-2012
Welcome to the forum.

First off, I never reccomend editing in-place, but you've had the wisdom to use backup files so that's good.

You don't need to escape the numbers, which is probably why it's complaining about backreferences. Don't think you need to escape <> either.

Another trick is you can put characters inside [] to 'escape' them, since sed won't treat most special characters in a range block as special. In some situations this is easier to read.

This seems to work.
Code:
$ cat data

BEFORE
<script LANGUAGE="JavaScript1.1">
<!--

function mMz()
{
 var mPz = "";
 for(var prop in this) {
 if ((prop.charAt(0) == '_' && prop.charAt(prop.length-1)=='_')
                      || ((typeof this[prop]) == 'function'))
 continue;
 if (mPz != "") mPz += '&';
 mPz += prop + ':' + escape(this[prop]);
 }
 var cookie = this.gHa + '=' + mPz;
 if (this._expiration_)
 cookie += '; expires=' + this._expiration_.toGMTString();
 if (this._path_) cookie += '; path=' + this._path_;
 if (this._domain_) cookie += '; domain=' + this._domain_;
 if (this._secure_) cookie += '; secure';

 this._document_.cookie = cookie;
}
//-->
</script>
AFTER

$ sed '/<script LANGUAGE="JavaScript1[.]1">/,/<[/]script>/d' data

BEFORE
AFTER

$

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 04-25-2012
works perfectly!

you are great! thank you so much

would you be also willing to give me some more help? here is the point:
i would like to clear some stuff between particular html tags and leave the tags intact. example

-------------quote
<td bgcolor="#eeeeee"><b><a href="http://www.kriyayoga.com/cgi-bin/anyboard.cgi?fvp=/family/sexuality_and_spirituality/&cmd=rA&cG=43">vandool</a></b> </td>
--------------unquote

i would like to leave only

<td bgcolor="#eeeeee"></b> </td>

there might be up to several such occurrences in a page.

best regards
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script Shell how's converting .html files

Hello everybody, :) I need some help with a school project that I have to create for the next week. :eek: So first, the aim of the Script is that I have a WebSite with a lot of .html / .shtml / .js / .css in one directory. That directory have few directory too but that's not so important,... (1 Reply)
Discussion started by: mariocrocop
1 Replies

2. UNIX for Beginners Questions & Answers

sed script to delete the last word after a last pattern match

Hi Guys , I am having a file as stated below File 1 sa0 -- i_core/i_core_apb/i_afe0_controller/U261/A sa0 -- i_core/i_core_apb/i_afe0_controller/U265/Z sa1 -- i_core/i_core_apb/i_afe0_controller/U265/A sa1 -- i_core/i_core_apb/i_afe0_controller/U268/Z sa1 -- ... (7 Replies)
Discussion started by: kshitij
7 Replies

3. Shell Programming and Scripting

Script needed to delete to the list of files in a directory based on last created & delete them

Hi My directory structure is as below. dir1, dir2, dir3 I have the list of files to be deleted in the below path as below. /staging/retain_for_2years/Cleanup/log $ ls -lrt total 0 drwxr-xr-x 2 nobody nobody 256 Mar 01 16:15 01-MAR-2015_SPDBS2 drwxr-xr-x 2 root ... (2 Replies)
Discussion started by: prasadn
2 Replies

4. Web Development

Html java script not working

Could anyone please let me know what went wrong with the below html code .. <html> <head> <script type="text/javascript"> function check(elem) { document.getElementById('mySelect1').disabled = !elem.selectedIndex; } </script> </head> <body> <form> <select id="mySelect"... (1 Reply)
Discussion started by: scriptscript
1 Replies

5. Shell Programming and Scripting

Need help creating a script to FTP files to a server and then delete the files that were transfered.

I am trying to FTP files to a Windows server through my Linux machine. I have setup the file transfer with no problems but am having problem deleting those files from the Linux box. My current non-working solution is below. Any ideas, anyone?? :wall: Please be gentle, I'm fairly new to this... (4 Replies)
Discussion started by: jmalfhs
4 Replies

6. Shell Programming and Scripting

Conditional search and delete using SED / Shell script

Hi, I want to perform a conditional search and remove my search string. Input string: "abcdaabcadgfaarstab" Character to search: "a" Condition: Remove all "a" in the input string except if it is "aa" Output string: "bcdaabcdgfaarstb" Can you please help me in this? (5 Replies)
Discussion started by: dominiclajs
5 Replies

7. Shell Programming and Scripting

Script to delete HTML tag

Guys, I have a little script that I got of the internet and that I use in Squid to block ads. I used that script with linux but now i have moved my servers to freebsd. I have a step learning curve there but it is fun: Back to the script issue. The script used to work i with linux but... (15 Replies)
Discussion started by: zongo
15 Replies

8. Shell Programming and Scripting

Need script to remove millions of tmp files in /html/cache/ directory

Hello, I just saw that on my vps (centOS) my oscommerce with a seo script has created millions of tmp files inside the /html/cache/ directory. I would need to remove all those files (millions), I tried via shell but the vps loads goes to very high and it hangs, is there some way to do a... (7 Replies)
Discussion started by: andymc1
7 Replies

9. Shell Programming and Scripting

sed of big html files

hi friends, i have to cut a large html file between tag " <!-- DEFACEMENTS ROWS -->" "<!-- DISCLAIMER FOOTER -->" and store cut data in other file please help me!!!! (2 Replies)
Discussion started by: praneshbmishra
2 Replies
Login or Register to Ask a Question