replace multiple lines in file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting replace multiple lines in file
# 1  
Old 08-18-2008
replace multiple lines in file

Hello
I am a beginner in shell script.

I was trying to find a way to replace multiple lines of a file with different set of multiple line.

Code:
    sed -n '/begin/,/end/p'  < sample1.txt >test.txt
        UNEDITED=`cat test.txt`
 
       vi test.txt

          EDITED=`cat test.txt`
        echo "$EDITED"

echo `sed s/$UNEDITED/$EDITED/  sample1.txt`

i tried the above code, no need to say it fails.
can someone please help me.

I apologies if this question is been asked before i tried to look but couldn't find it

Last edited by nox; 08-18-2008 at 09:45 AM..
# 2  
Old 08-18-2008
Gives us an example "before" and "after" file
# 3  
Old 08-18-2008
Quote:
Originally Posted by jim mcnamara
Gives us an example "before" and "after" file
This is the difficult part. This is supposed to be for an configuration file.
so lets say,

before:
Code:
SECTION_A
property1  attribute1="zzz", attribute2="aaa",
                  attribute3="ccc"

property2  attribute1="qqq", attribute2="aaa",
                  attribute3="xx"

SECTION_B
property3  attribute1="zxx", attribute2="aaa",
                  attribute3="c"

property4  attribute1="zzz", attribute2="aaa",
                   attribute3="ccc"

so there is "vi" command given for the file. so in this case the code for editing only the "section_a" of the configuration
Code:
    sed -n '/SECTION_A/,/SECTION_B/p'  < sample1.txt >test.txt
        UNEDITED=`cat test.txt`
 
       vi test.txt

          EDITED=`cat test.txt`
        echo "$EDITED"

echo `sed s/$UNEDITED/$EDITED/  sample1.m`

so in some sense i want to edit specific section of configuration file. save it it temp file. and then updated the edited section.

The whole point of going through this exercise was to give user chance to update only the section he wants and hiding the rest of configuration temporarily.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removing multiple lines from input file, if multiple lines match a pattern.

GM, I have an issue at work, which requires a simple solution. But, after multiple attempts, I have not been able to hit on the code needed. I am assuming that sed, awk or even perl could do what I need. I have an application that adds extra blank page feeds, for multiple reports, when... (7 Replies)
Discussion started by: jxfish2
7 Replies

2. Shell Programming and Scripting

Replace a string with multiple lines

Hello Guys, I need to replace a string with multiple lines. For eg:- ABC,DEF,GHI,JKL,MNO,PQR,STU need to convert the above as below:- ABC,DEF, GHI1 GHI2 GHI3, JKL,MNO, PQR1 PQR2 PQR3, STU i have tried using code as:- (2 Replies)
Discussion started by: jassi10781
2 Replies

3. Shell Programming and Scripting

Replace multiple lines through sed

Hi All, I have a input file as sample below <this is not starting of file> record line1 line2 line3 end line4 line5 record line6 line7 line8 my requirement is this, i want to select a pattern between first record and end, whatever is written between first record and end. and... (0 Replies)
Discussion started by: adgangwar
0 Replies

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

5. Shell Programming and Scripting

search and replace, when found, delete multiple lines, add new set of lines?

hey guys, I tried searching but most 'search and replace' questions are related to one liners. Say I have a file to be replaced that has the following: $ cat testing.txt TESTING AAA BBB CCC DDD EEE FFF GGG HHH ENDTESTING This is the input file: (3 Replies)
Discussion started by: DeuceLee
3 Replies

6. Shell Programming and Scripting

replace (sed?) a string in file with multiple lines (string) from variable

Can someone tell me how I can do this? e.g: a=$(echo -e wert trewt ertert ertert ertert erttert erterte rterter tertertert ert) How do i replace the STRING with $a? I try this: sed -i 's/STRING/'"$a"'/g' filename.ext but this don' t work (2 Replies)
Discussion started by: jforce
2 Replies

7. Shell Programming and Scripting

replace a string with contents of a txt file containing multiple lines of strings

Hello everyone, ive been trying to replace a string "kw01" in an xml file with the contents of a txt file having multiple lines. im a unix newbie and all the sed combinations i tried resulted to being garbled. Below is the contents of the txt file: RAISEDATTIME --------------------... (13 Replies)
Discussion started by: 4dirk1
13 Replies

8. Shell Programming and Scripting

replace (sed?) a single line/string in file with multiple lines (string) from another file??

Can someone tell me how I can do this? e.g: Say file1.txt contains: today is monday the 22 of NOVEMBER 2010 and file2.txt contains: the 11th month of How do i replace the word NOVEMBER with (5 Replies)
Discussion started by: tuathan
5 Replies

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

10. Shell Programming and Scripting

replace multiple lines in multiple files

i have to search a string and replace with multiple lines. example Input echo 'sample text' echo 'college days' output echo 'sample text' echo 'information on students' echo 'emp number' echo 'holidays' i have to search a word college and replace the multiple lines i have... (1 Reply)
Discussion started by: unihp1
1 Replies
Login or Register to Ask a Question