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.:slight_smile:

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

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

sed '2r YYY.dat' XXX.dat > output_file

sed :

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

sed '3r b.txt' a.txt