adding text to a file between lines


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting adding text to a file between lines
# 1  
Old 10-05-2006
adding text to a file between lines

Suppose content of my first file:

first line
second line
third line


How can i insert text between "first line" & "second Iline"

Any help?????/
# 2  
Old 10-05-2006
Alternative in Python:

Code:
#!/usr/bin/python
data = open("file").readlines()
data.insert(1,'inserted line\n')
print ''.join(data)

Output:
Code:
first line
inserted line
second line
third line

# 3  
Old 10-05-2006
Code:
sed "/first line/i\\
hi buddy\\
how r u ?\\
Gud morning" file

or if you want to add contents from a file use this

Code:
sed "/first line/r secondfile" file

# 4  
Old 10-09-2006
Quote:
Originally Posted by anbu23
Code:
sed "/first line/i\\
hi buddy\\
how r u ?\\
Gud morning" file

or if you want to add contents from a file use this

Code:
sed "/first line/r secondfile" file

------------------------------------------
--------------------------------

if i have a file of 1000 lines.....

how can I give set a particular in the sed command....this is ok for a file with few lines...hope u understand the concern.....
# 5  
Old 10-09-2006
text deleted..


Sorry.. Understood the requirement wrongly
# 6  
Old 10-09-2006
Quote:
Originally Posted by bishweshwar
------------------------------------------
--------------------------------

if i have a file of 1000 lines.....

how can I give set a particular in the sed command....this is ok for a file with few lines...hope u understand the concern.....
Can you explain with example?
# 7  
Old 10-09-2006
Simply:
Code:
sed '2i Hello' file

This will return
Code:
first line
Hello
second line
third line

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Match text to lines in a file, iterate backwards until text or text substring matches, print to file

hi all, trying this using shell/bash with sed/awk/grep I have two files, one containing one column, the other containing multiple columns (comma delimited). file1.txt abc12345 def12345 ghi54321 ... file2.txt abc1,text1,texta abc,text2,textb def123,text3,textc gh,text4,textd... (6 Replies)
Discussion started by: shogun1970
6 Replies

2. Shell Programming and Scripting

Adding lines to a large file

Hello, I have a relatively large text file (25,000K) consisting of records of data. For each record, I need to create a new line based on what is already there. Every record has a block that looks like, M END > <ID> 1 > <SOURCE> KEGG > <SOURCE_ID> C00002 > <NAME> ATP;... (4 Replies)
Discussion started by: LMHmedchem
4 Replies

3. Shell Programming and Scripting

Adding lines at a particular location in a file.

Hi Experts, Let us take a text file,say items.txt having the following data jar bottle gum tube cereal bag I want to add the content of items.txt to another file say #many lines not necessary ingredients #many line not necesary ingredients I want to append the data in... (3 Replies)
Discussion started by: Pradeep_1990
3 Replies

4. Shell Programming and Scripting

Adding new lines to a file + adding suffix to a pattern

I need some help with adding lines to file and substitute a pattern. Ok I have a file: #cat names.txt name: John Doe stationed: 1 name: Michael Sweets stationed: 41 . . . And would like to change it to: name: John Doe employed permanently stationed: 1-office (7 Replies)
Discussion started by: hemo21
7 Replies

5. Shell Programming and Scripting

[bash help]Adding multiple lines of text into a specific spot into a text file

I am attempting to insert multiple lines of text into a specific place in a text file based on the lines above or below it. For example, Here is a portion of a zone file. IN NS ns1.domain.tld. IN NS ns2.domain.tld. IN ... (2 Replies)
Discussion started by: cdn_humbucker
2 Replies

6. Shell Programming and Scripting

Adding text to file on certain lines with(special characters)

I need to add "new lines" of text with special characters, to specific lines in the file. There are 3 modifications needed. Been testing 2 here without success. #!/usr/bin/perl use FileHandle; $file=FileHandle->new; $FILENAME="/opt/etc/usr/file.txt"; $file->open ("<$FILENAME") or die... (13 Replies)
Discussion started by: A4ron4perl
13 Replies

7. Shell Programming and Scripting

Adding strings to lines in a file

Hi all, I have a positional text file that comes from some source application. Before it is processed by destination application I have to add some header (suffix) to every record(line) in the file. e.g. Actual File ............... AccountDetails AcNO Name Amount 1234 John 26578 5678... (3 Replies)
Discussion started by: sharath160
3 Replies

8. UNIX for Dummies Questions & Answers

Adding lines and columns to a file

Hi everybody, I've got two simples file1 like: aaa aaa aaa bbb bbb bbb ccc ccc ccc and file2 like: 111 111 111 222 222 222 333 333 333 I need to: 1) add a line say "new line" as the first line of the file 2)add a column from file2 (say column3) to file1; the new column should... (14 Replies)
Discussion started by: zajtat
14 Replies

9. Shell Programming and Scripting

Adding specific text and spaces to each line in a text file

Hi, I wanted to add specific text to each row in a text file containing three rows. Example: 0 8 7 6 5 5 7 8 9 0 7 9 7 8 9 0 1 2 And I want to add a 21 at the beginning of the first row, and blank spaces at the beginning of the second two rows. To get this: 21 0 8 7 6 5 5 7 8... (4 Replies)
Discussion started by: hertingm
4 Replies

10. Shell Programming and Scripting

adding text to the end of lines containing a word

I'm new to shell scripting, and need to add a series of commands to the ends of certain lines of text that contain a keyword. Any easy way to do this? Thanks (2 Replies)
Discussion started by: ironwall
2 Replies
Login or Register to Ask a Question