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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed to replace a line with 2 or more different lines in same file
# 8  
Old 04-06-2012
awk

Quote:
Originally Posted by vivek d r
yeah the files are there.. the line is not executing.. no error msgs or anything...

Code:
[root@vivek]# awk 'BEGIN{i=1;while(getline line <"attributee.txt"){a[i]=line;i++;}}/<\/complexType >/{for(j=1;j<=length(a);j++){print a[j];}}1' sample.xsd
 
^[^[^[
 (Cntrl+C pressedabove is i tried to press ESC... )

after i execute the line nothing happens.. the shell promt wont return..
It seems to be the problem with the "attributee.txt". Please do check the file name again and get back to me.

Cheers,
RangaSmilie
This User Gave Thanks to rangarasan For This Post:
# 9  
Old 04-06-2012
thanks a lot...
Quote:
It seems to be the problem with the "attributee.txt". Please do check the file name again and get back to me.
this worked.. :-)
# 10  
Old 04-06-2012
Code:
$ cat infile 
<complexTypename="abc">
bklah vlah
blah 
blha blha blah
blha
 
</complexType >
<complexTypename="def">
bklah vlah
blah 
blha blha blah
blha
 
</complexType >

Code:
$ cat insert.txt 
<attribute name="123" type="string"></attribute>
<attribute name="456" type="string"></attribute>
</complexType >

Code:
$ sed 's/<\/complexType >/cat insert.txt/eg' infile 
<complexTypename="abc">
bklah vlah
blah 
blha blha blah
blha
 
<attribute name="123" type="string"></attribute>
<attribute name="456" type="string"></attribute>
</complexType >
<complexTypename="def">
bklah vlah
blah 
blha blha blah
blha
 
<attribute name="123" type="string"></attribute>
<attribute name="456" type="string"></attribute>
</complexType >

These 2 Users Gave Thanks to complex.invoke For This Post:
# 11  
Old 04-06-2012
The e-flag is a GNU sed extension
This User Gave Thanks to Scrutinizer For This Post:
# 12  
Old 04-06-2012
Using the input files from the previous post,
something like this may work with not so modern sed implementations too
and should be efficient:

Code:
sed '/<\/complexType >/{
r insert.txt
D
}' infile

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

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

sed replace to rename each line a file

Have a file in this format This is line one ; line_one This is line two ; line_two This is line three ; line_three This is line four ; line four. I'm trying to make each line a new file called line_one line_two line_three line_four. Tried using split -1 but then I'm back needing to rename... (3 Replies)
Discussion started by: jimmyf
3 Replies

2. Shell Programming and Scripting

Grep 2 consecutive lines and replace the second line in a file

I have a file lake this cat ex1.txt </DISCOUNTS> <B2B_SPECIFICATION elem="0"> <B2B_SPECIFICATION elem="0"> <DESCR>Netti 2 </DESCR> <NUMBER>D02021507505</NUMBER> </B2B_SPECIFICATION> <B2B_SPECIFICATION elem="1"> <DESCR>Puhepaketti</DESCR>... (2 Replies)
Discussion started by: Dhoni
2 Replies

3. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 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

sed to replace a line with multi lines from a var

I am trying to find a line in a file ("Replace_Flag") and replace it with a variable which hold a multi lined file. myVar=`cat myfile` sed -e 's/Replace_Flag/'$myVar'/' /pathto/test.file myfile: cat dog boy girl mouse house test.file: football hockey Replace_Flag baseball ... (4 Replies)
Discussion started by: bblondin
4 Replies

6. Shell Programming and Scripting

sed or awk to replace a value in a certain line from another file containing a string

Hi experts, In my text file I have the following alot of lines like below. input.k is as follows. 2684717 -194.7050476 64.2345581 150.6500092 0 0 2684718 -213.1575623 62.7032242 150.6500092 0 0 *INCLUDE $# filename... (3 Replies)
Discussion started by: hamnsan
3 Replies

7. Shell Programming and Scripting

sed to replace a line with modified line in same file

i have few lines in a file... i am reading them in a while loop so a particular line is held is $line1.. consider a modified line is held in $line2.... i want to replace $line1 with $line2 in the same file... how to do it..? i have come up till the below code sed "s/$line1/$line2/g" tmpfile.sql... (5 Replies)
Discussion started by: vivek d r
5 Replies

8. Shell Programming and Scripting

sed replace characters in next line with input from a file

Hi, I have a set of strings in filea. I want to search string xyz in fileb and replace next line in file b with the content from filea. #cat filea abc def ghi #cat fileb asdkjdslka sajljskdjoi xyzjjjjkko aaaaaaaa bbbbbbbb cccccccc xyzsdsajd dddddddd eeeeeeee (2 Replies)
Discussion started by: anilvk
2 Replies

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

10. Shell Programming and Scripting

Replace a line in a file with sed

Hi, I am trying to write a script that will replace "PermitEmptyPasswords yes" with "PermitEmptyPasswords no". The following does not seem to work: - sed 's!/"PermitEmptyPasswords yes"/!/"PermitEmptyPasswords no"/!' Appreciate any ideas. Thanks (2 Replies)
Discussion started by: mtech3
2 Replies
Login or Register to Ask a Question