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
# 1  
Old 04-06-2012
sed to replace a line with 2 or more different lines in same file

i have something like this...
Code:
cat filename.txt
<complexTypename="abc">
bklah vlah
blah 
blha blha blah
blha
 
</complexType >
<complexTypename="def">
bklah vlah
blah 
blha blha blah
blha
 
</complexType >
.
.
.and so on.. its a very large file

no i need to replace end of complex type that is
Code:
</complexType >

with
below three lines
Code:
 
<attribute name="123" type="string"></attribute>

Code:
<attribute name="456" type="string"></attribute>
</complexType >

or you could say put the two attributes lines just above </complexType >

this could be easily done in notepad++ but i want to do it in shell... and this attributes can be lenghty and cover different lines too..
like
Code:
 
<attribute name="456" type="string">
blah blha blha
blha blha
</attribute>


any help is deeply appreciated :-)

# 2  
Old 04-06-2012
awk

Hi,

Try this one,

Code:
awk '/<\/complexType >/{print "<attribute name=\"123\" type=\"string\"></attribute>\n<attribute name=\"456\" type=\"string\"></attribute>";}1' file

Change the print statement based on your requirement.

Cheers,
Ranga Smilie
This User Gave Thanks to rangarasan For This Post:
# 3  
Old 04-06-2012
thanks the code is working :-)
whatif i have the below content in a file called attribute.txt.
Code:
 
<attribute name="123" type="string"></attribute>
<attribute name="456" type="string"></attribute>
<attribute name="456"
blah blah
</attribute>

how to use this to perfrom same function as the awk statement above...
# 4  
Old 04-06-2012
awk

Hi,

Try this one,

Code:
awk 'BEGIN{i=1;while(getline line <"attribute.txt"){a[i]=line;i++;}}/<\/complexType >/{for(j=1;j<=length(a);j++){print a[j];}}1' file

The above code will read attribute.txt file and print it before
Code:
</complexType>

found while traversing file. am i come over your requirement ?

Cheers,
Ranga Smilie
This User Gave Thanks to rangarasan For This Post:
# 5  
Old 04-06-2012
hmmm the above code doesnt execute... i think some quote is missing :-/
# 6  
Old 04-06-2012
awk

Make sure that attribute.txt file and input file should be in the current working directory.
Its working fine for me Smilie
This User Gave Thanks to rangarasan For This Post:
# 7  
Old 04-06-2012
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..
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