Inserting file into another file at a specified line number


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Inserting file into another file at a specified line number
# 1  
Old 10-17-2008
Bug Inserting file into another file at a specified line number

Hi,

I have two files XXX.dat and YYY.dat and their contents are as follows.

The contents of XXX.dat(The line numbers are just for reference)

1) Mango
2) Banana
3) Papaya
4) Apple
5) Pomegranate


The contents of YYY.dat

1) Custard apple
2) Grape
3) Pine apple


Now I want to insert YYY.dat into XXX.dat at a specified line number,say 2.The line number is not fixed.It shall be any of the lines in XXX.dat

Please help me asap.Smilie
# 2  
Old 10-17-2008
1) read again the rules:
The UNIX and Linux Forums - Forum Rules
( Rule 2 Be patient! - So no Please help me asap... thanks)

2)
1 Solution:
You will need to append so ">>" will be used
and the commands head and tail, and see how by using man
And you would have to write a test to accept the line number for argument...

Remark:
Since your lines are numbered, what will to with them?...( out of sequence...)


3)
Share your solution with your peers, and ask them to give their opinion
(I believe here will you see then other solutions with perhaps some explained algorithms...) or the issue you have
# 3  
Old 10-20-2008
I normally use sed in this context

## print line number 1 to 2
sed -n '1,2p' XXX.dat > output_file

## cat another file
cat YYY.dat >> output_file

## print remaining lines from file 1
sed '1,2d' XXX.dat >> output_file
# 4  
Old 10-20-2008
Code:
sed '2r YYY.dat' XXX.dat > output_file

# 5  
Old 10-20-2008
sed :


below script will append the content of file b.txt to the 3rd line of a.txt, think this can address your requirements.
Code:
sed '3r b.txt' a.txt

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Inserting a line to a file

Hi, consider a file called mobile.txt as follows: For type lovers, add a new line at the end of it by copying its previous line and add a +1 to the field1, field2 Additionally, there are only 3 plans available to lovers type, so it should not work for lovers type already having 3 lines under... (11 Replies)
Discussion started by: Gautham
11 Replies

2. Shell Programming and Scripting

Inserting a new line into an already existing file

hello .. I am new to perl scripting, I have a text file, and would like to insert 3 new lines into the same file at different line numbers using perl scripting. Any Help regarding this will be very useful. say the file is sample.txt, contents are aaaaa bbbb ccccc dddd eeeee ffffffff... (4 Replies)
Discussion started by: hemalathak10
4 Replies

3. Shell Programming and Scripting

Inserting file after specific line in another file

Im attempting to insert the contents of File1 at a specific point of File2. File1 AD004 0 1 0 0 1 0 1 0 0 1 0 1 0 1 0 1 File2 AA001 0 1 0 0 1 0 1 0 0 1 0 1 0 1 0 1 AB002 0 1 0 0 1 0 1 0 0 1 0 1 0 1 0 1 AC003 0 1 0 0 1 0 1 0 0 1 0 1 0 1 0 1 Result AA001 0 1 0 0 1 0 1 0 0 1 0 1 0 1 0 1... (13 Replies)
Discussion started by: ncwxpanther
13 Replies

4. UNIX for Dummies Questions & Answers

Inserting a sequential number into a field on a flat file

I have a csv flatfile with a few million rows. I need to replace a field (field number is 85) in the file with a sequential number. As an example, let's assume there are only 4 fields in the file: A,A,,32 A,A,,27 A,B,,43 C,C,,354 If I wanted to amend the 3rd field in this way my... (2 Replies)
Discussion started by: BristolSmithy
2 Replies

5. Shell Programming and Scripting

Inserting IP in one line of a file

Hi, I have this line: ip=111.222.133.144,mac=00:16:3E:2A:08:3C,vifname=veth360','ip=10.2.3.4,vifname=veth360a' ^ | ------- I want to insert this IP 144.133.222.111 between "144"... (4 Replies)
Discussion started by: iga3725
4 Replies

6. Ubuntu

Inserting a header with column number to a 1.6 GB file with special spacing

Hi; I've been searching posts to find a solution to what I'm trying to do, but I've have NOT found anything yet. I have a file (file1) with 300K columns and 1411 rows, the columns don't have a column no. header (No header at all) and I'm trying to fetch the information from specific columns.... (3 Replies)
Discussion started by: sogi
3 Replies

7. Shell Programming and Scripting

inserting line to a file

I have posted it previously but somehow could not delete the previous post.I felt i could not explain the problem statement well. Here t goes.I have a file say File1. Now i need a specific pattern from the lines to be added to the other line. File: red blue green ABC.txt@ABC END black... (1 Reply)
Discussion started by: ngupta
1 Replies

8. Shell Programming and Scripting

Need Help in Inserting a new line in a file using PERL

I need a perl script to find and replace a specific pattern in a file to a new line. BAsically I have a single line data in a file with 10 mb to 200 MB. I want to put a new line based on a specific pattern to open the file in Excel / Access. Following is the sample data in a file ... (1 Reply)
Discussion started by: portalfaq
1 Replies

9. Shell Programming and Scripting

inserting a new line in a file

I'm sure you guys have answered this elsewhere but I can't seem to find where so here goes. #!/bin/bash n=120 a=$(sed '120q;d' energy.xvg) while ;do a=$(sed $n'q;d' energy.xvg) echo "$a \n" > newfile n=$(($n+100)) done exit 0 that script should read the file energy.xvg, start at... (1 Reply)
Discussion started by: gelitini
1 Replies

10. Shell Programming and Scripting

Inserting New Line in File using Sed ??

Dear All, I have a file called football where i have a list of 11 players each on different lines. I wish to add a name of another player on the first line. I have created a file called footballscript in vi writing the following sed command to achieve this ... cat football | sed -e '1 i\... (4 Replies)
Discussion started by: Mary_xxx
4 Replies
Login or Register to Ask a Question