how to replace one line in text with several lines


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers how to replace one line in text with several lines
# 1  
Old 01-18-2007
how to replace one line in text with several lines

Hi,
I need to replace a specific line in a text (eg the line contains the word MYSERVER) with a number of lines (let's say 4) in which the word MYSERVER will be replaced with
server1 in the first line
server2 in the second line
server3 in the 3rd line
server4 in the 4th line

The original text would be something like
lline1
line2
line3

THIS IS THE LINE WITH MYSERVER
line4
line5


The resulting text should be like:
lline1
line2
line3

THIS IS THE LINE WITH server1
THIS IS THE LINE WITH server2
THIS IS THE LINE WITH server3
THIS IS THE LINE WITH server4

line4
line5


If anyone could help with this it would be great!
Thank you.
# 2  
Old 01-18-2007
Code:
sed "/MYSERVER/c\\
THIS IS THE LINE WITH server1\\
THIS IS THE LINE WITH server2\\
THIS IS THE LINE WITH server3\\
THIS IS THE LINE WITH server4" file

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to find text and replace next line.?

Have multiple files with the same format and certain text is the same. Have specific text to search for and then need to replace the next line. Can search on the text DEVICE and multiple lines will be found. The line after each DEVICE line has {. Want to replace the line { with {-someadditiontext.... (2 Replies)
Discussion started by: bigdaddywags
2 Replies

2. UNIX for Dummies Questions & Answers

Replace lines of two files by the corresponding line numbers.

I want to replace lines. The files 1 are (separated by \t) Gm01 phytozome9_0 three_prime_UTR 70641 70759 . - . ID=PAC:26323927.three_prime_UTR.1;Parent=PAC:26323927;pacid=26323927 Gm01 phytozome9_0 three_prime_UTR 90230 90692 . - . ... (1 Reply)
Discussion started by: grace_shen
1 Replies

3. Shell Programming and Scripting

Search text and replace line next to it

I have a file which requires modification via a shell script. Need to do the following: 0. take input from user for new text. 1. search for a keyword in the file. 2. replace the line next to this to this keyword with user supplied input. for e.g., my file has the following text: (some... (7 Replies)
Discussion started by: chingupt
7 Replies

4. Shell Programming and Scripting

[sed] Replace one line with two lines

Literally cannot get this one, guys. Single line replacement is simple, but I am not understanding the correct syntax for including a new line feed into the substitution part. Here's what I got. (Cannot use perl) #!/bin/sh set -f #Start Perms export HOME=/home/test_user # End Perms... (6 Replies)
Discussion started by: Parallax
6 Replies

5. Shell Programming and Scripting

How to replace a text containing new lines using sed or any other method?

Hi, i want to replace "Hi How are You when did you go to delhi" to "Hi How are you when did you come from delhi" in a file. Any idea how to do it? (2 Replies)
Discussion started by: abhitanshu
2 Replies

6. Shell Programming and Scripting

sed to replace a line with 2 or more different lines in same file

i have something like this... cat filename.txt <complexType name="abc"> bklah vlah blah blha blha blah blha </complexType > <complexType name="def"> bklah vlah blah blha blha blah blha </complexType > . . .and so on.. its a very large file (11 Replies)
Discussion started by: vivek d r
11 Replies

7. UNIX for Dummies Questions & Answers

how to replace a text of line with a comment line

I want to replace this line : "test compare visible] true" and make it "#test compare visible] true". How can I do it ? And it should be checked in many sub folder files also. (6 Replies)
Discussion started by: manoj.b
6 Replies

8. UNIX for Dummies Questions & Answers

how to use vi to replace specific line of text only

hi all, i am new bee to Unix. i know how to replace text for range of lines in vi for e.g. replace | with |||| for line 4 through 7 using vi : 4,7s/|/||||/g but i have to replace | with |||| only for line no 4, 7 and 10 using vi only!!! your help will be appreciated! thanks,... (4 Replies)
Discussion started by: pranav.pandya
4 Replies

9. UNIX for Advanced & Expert Users

Replace the text between two lines with different text

I have a file which contains the following data. Parameters "CParameters" BEGIN DSSUBRECORD Name "TgtDB" Prompt "TgtDB" Default "edwdev" ParamType "0" ParamLength "0" ParamScale "0" END DSSUBRECORD MetaBag... (6 Replies)
Discussion started by: ukatru
6 Replies

10. UNIX for Dummies Questions & Answers

Replace line of text in a file

I need to change the first line a file from "HDR,{any_number}",,,,, where {any_number} is incremental for each file to HDR,{any_number} Can someone please suggest an easy way please Regards Col (3 Replies)
Discussion started by: colinchase
3 Replies
Login or Register to Ask a Question