add few lines to a file in unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting add few lines to a file in unix
# 1  
Old 08-13-2009
add few lines to a file in unix

Hi,

I have an xml file
<Disp
x=y a=b
Disp/>
<Disp
q=1 w=2
Disp/>
.....

I want to add a new set in between like this for a set of 100 files.
<Disp
x=y a=b
Disp/>
<Disp
k=1 z=2
Disp/>

<Disp
q=1 w=2
Disp/>
.....

is it possible to achieve through sed or i should go with some thing else?
# 2  
Old 08-13-2009
you don't say what is the condition to know in which line insert.
This is a example for insert in a specific line (line 2) of a file using sed:
Code:
$more file_in
line1
line2
line3
$cat file_in | sed '2 i <Disp\nk=1 z=2\n Disp/>' >file_out
$more file_out
line1
<Disp
k=1 z=2
 Disp/>
line2
line3

# 3  
Old 08-13-2009
Hi,

Please can you specify the condition were the line needs to get inserted or is it that it can be inserted in any place between the first and the last.

Cheers,
Shazin
# 4  
Old 08-13-2009
Hi,

<Disp apple
x=y a=b
Disp/>
<Disp grapes
k=1 z=2
Disp/>

<Disp orange
q=1 w=2
Disp/>

I have a sequence of such <Disp> tags in my file, I want to add the Disp tag with name grapes above the disp tag of orange. This is the case for all 100 files.
The orange tag may present anywhere in the file.

I am seeing for a script which inserts the 3 lines of the grapes tag above the orange tag.
# 5  
Old 08-13-2009
in reality the solutio i propose you isn't and insert because use 's' that means sustitution:
Code:
$more file1
<Disp apple
x=y a=b
Disp/>
<Disp orange
q=1 w=2
Disp/>
$cat file1 | sed "s/<Disp orange/<Disp grapes\nk=1 z=2\nDisp\/>\n<Disp orange/"
<Disp apple
x=y a=b
Disp/>
<Disp grapes
k=1 z=2
Disp/>
<Disp orange
q=1 w=2
Disp/>

# 6  
Old 08-13-2009
Output i got for
$cat file1 | sed "s/<Disp orange/<Disp grapes\nk=1 z=2\nDisp\/>\n<Disp orange/"
Output:
<Disp grapesnk=1 z=2nDisp/>n<Disp orange
It dosen't recognize /n for new line.
# 7  
Old 08-13-2009
in GNU/Linux work fine, now i probe in HP-UX and i got the same result, command print n...
is like in this part of the argument the comand don't interpret \n....

---------- Post updated at 11:46 AM ---------- Previous update was at 11:30 AM ----------

i try to escape de \n in all forms that i know but with the same result...
i find a solution in Internet, i don't like so much but it work fine un UNIX plataform.You need to write the script exactly like i put:

Code:
$more sed1.sh
cat file11 | sed 's/<Disp orange/<Disp grapes\
k=1 z=2\
Disp\/>\
<Disp orange/'

in this URL you have and explication:
Sed - An Introduction and Tutorial

I supose the version of sed in linux support to put \n in left side of a substitute command...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Add 3 new lines for every line in a file

I have a file that I need to add 3 new lines of text for every line in the file - when I attempt the sed scripting below I keep getting 'command garbled' errors. Any suggestions would be greatly appreciated #!/bin/ksh list=`cat /export/home/list` for i in $list do sed ' a \... (7 Replies)
Discussion started by: bjdamon
7 Replies

2. UNIX for Dummies Questions & Answers

Add strings from one file at the end of specific lines in text file

Hello All, this is my first post so I don't know if I am doing this right. I would like to append entries from a series of strings (contained in a text file) consecutively at the end of specifically labeled lines in another file. As an example: - the file that contains the values to be... (3 Replies)
Discussion started by: gus74
3 Replies

3. Shell Programming and Scripting

How to add two common lines on entire file

Hi Hi i have 500 lines of file,each line i need to add another two lines also need to separate with one line for every 3 lines after adding two lines.How to achieve this using shell? For example: Input file : dn: uid=apple,dc=example,dc=com dn: uid=ball,dc=example,dc=com output:... (4 Replies)
Discussion started by: buzzme
4 Replies

4. Shell Programming and Scripting

Extract some lines from one file and add those lines to current file

hi, i have two files. file1.sh echo "unix" echo "linux" file2.sh echo "unix linux forums" now the output i need is $./file2.sh unix linux forums (3 Replies)
Discussion started by: snreddy_gopu
3 Replies

5. Shell Programming and Scripting

How to add lines of a file and average them

I'm reading in numbers from a file and trying to add them together. Here is the code so far. I know the 1+2+3.... part is wrong. The file has five numbers in it with each number on its own line. The numbers are decimals if that matters. Thanks. while read EachLine do echo $EachLine done <... (6 Replies)
Discussion started by: AxlVanDamme
6 Replies

6. Shell Programming and Scripting

XML file: add 100 lines

Hi Gurus, I have XML file which i want to add 100 lines.I have no idea in xml how to add or modify data .Any help should be appreciated. Thanks, Akil (6 Replies)
Discussion started by: akil
6 Replies

7. UNIX for Advanced & Expert Users

Add Comments to the specifi lines i na file

I have a requirement like below.I need to Comment some lines in a file. File contains following information. { attribute1 attribute2 atrribute3 attribute4 attribute5 attribute6 attribute7 } I have a requirement like some times i need to comment lines 3 to before '}' and some... (1 Reply)
Discussion started by: ukatru
1 Replies

8. Shell Programming and Scripting

add lines in file with perl

How to search string like: a and replace to a a a : : a in a file with perl? Thanks, Grace (6 Replies)
Discussion started by: jinsh
6 Replies

9. Shell Programming and Scripting

Add Lines in middle of file

I need to add a new lines with certain data, to an existing file, every 5 lines in the file. There is no way to find any distinct charater pattern so I will have to do a line count and then insert the new line. I think using awk or sed is what I need to do. Any help is appreciated. Kunder (2 Replies)
Discussion started by: kunder
2 Replies

10. Shell Programming and Scripting

Add lines into file

Hi, i have a file like: 00:00 8 00:01 2 00:04 5 00:07 10 . . . and i need to add the other minutes with value 0 and have a file like: 00:00 8 00:01 2 00:02 0 00:03 0 00:04 5 00:05 0 00:06 ... (13 Replies)
Discussion started by: Lestat
13 Replies
Login or Register to Ask a Question