Insert content of a file to another file at a line number which is given by third file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert content of a file to another file at a line number which is given by third file
# 1  
Old 09-29-2016
Linux Insert content of a file to another file at a line number which is given by third file

Hi friends, here is my problem.

I have three files like this..

cat file1.txt
=======
Code:
unix is best 
unix is best 
linux is best
unix is best
linux is best
linux is best
unix is best
unix is best

cat file2.txt
========
Code:
Windows performs better
Mac OS performs better
Windows performs better
Mac OS performs better

cat file3.txt
==========
Code:
6

I want to insert content of file2.txt into a file1.txt at a linenumber given by file3.txt.. Smilie

OUTPUT should be
=============
Code:
unix is best 
unix is best 
linux is best
unix is best
linux is best
Windows performs better
Mac OS performs better
Windows performs better
Mac OS performs better
linux is best
unix is best
unix is best

can you please suggest me one liner shell command to achieve above task..



Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 09-29-2016 at 06:34 AM.. Reason: Added CODE tags.
# 2  
Old 09-29-2016
Hi,
under linux and shell bash:
Code:
$ sed -e "$((`<file3.txt` -1))rfile2.txt" file1.txt
unix is best
unix is best
linux is best
unix is best
linux is best
Windows performs better
Mac OS performs better
Windows performs better
Mac OS performs better
linux is best
unix is best
unix is best

Regards.
These 2 Users Gave Thanks to disedorgue For This Post:
# 3  
Old 09-29-2016
Hello Jagadeesh Kumar,

Welcome to forums, please use code tags as per forum rules for your codes/Inputs/commands which you are using into your posts. Following may help you in same.
Code:
awk -vval=`cat Input_file3.txt` 'NR==val{system("cat Input_file2.txt");} 1'  Input_file1.txt

Output will be as follows.
Code:
unix is best
unix is best
linux is best
unix is best
linux is best
Windows performs better
Mac OS performs better
Windows performs better
Mac OS performs better
linux is best
unix is best
unix is best

Thanks,
R. Singh
These 2 Users Gave Thanks to RavinderSingh13 For This Post:
# 4  
Old 09-29-2016
Try also
Code:
awk 'NR == 1 {LC = $0; next} FNR == LC {while (0 < getline X < FI) print X} 1' file3 FI="file2" file1
unix is best 
unix is best 
linux is best
unix is best
linux is best
Windows performs better
Mac OS performs better
Windows performs better
Mac OS performs better
linux is best
unix is best
unix is best

These 2 Users Gave Thanks to RudiC For This Post:
# 5  
Old 09-29-2016
With a recent shell, try
Code:
sed "$(($(cat file3)-1))rfile2"  file1

This User Gave Thanks to RudiC For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Insert the line number from text file to filename output

Hi everyone :) I have a file "words.txt" containing hundreds of lines of text. Each line contains a slogan. Using the code below i am able to generate an image with the slogan text from each line. The image filename is saved matching the last word on each line. Example: Line 1: We do... (2 Replies)
Discussion started by: martinsmith
2 Replies

2. UNIX for Beginners Questions & Answers

Insert content from file 1 to file 2 in specific criteria meet

Hi , I'm looking for some code that can copy and paste form file1 to file2 with 2 criterial meet. file1: test "sp-j1" test "sp-j2" test "sp-j3" test "sp-j4" file2: sub Pre_Shorts1 (Status_Code, Message$) global Status !if Message$ <> "" then print... (3 Replies)
Discussion started by: kttan
3 Replies

3. Shell Programming and Scripting

Insert content of a file right after pattern in another file

suppose i have original file: original.txt: hello how are you you are wonderful what time is it I went to the store last night. and some apple juice then i have another file: anotherfile.txt: with my friends mary, john and harry. We had a great time. We bought food Suppose... (1 Reply)
Discussion started by: SkySmart
1 Replies

4. Shell Programming and Scripting

Insert content of file before the first occurrence of a line starts with a pattern in another file

Hi all, I'm new to scripting.. facing some problems while inserting content of a file into another file... I want to insert content of a file (file2) into file1, before first occurrence of "line starts with pattern" in file1 file1 ====== working on linux its unix world working on... (14 Replies)
Discussion started by: Jagadeesh Kumar
14 Replies

5. Shell Programming and Scripting

Insert content of a file into another file before given pattern

I need to insert file x2 into x1 right before first BBB line. $ cat x1 AAA 1 AAA 2 AAA 3 BBB 1 BBB 2 BBB 3 $ cat x2 XXX - insert 1 XXX - insert 2 I need to get AAA 1 AAA 2 AAA 3 XXX - insert 1 XXX - insert 2 BBB 1 (2 Replies)
Discussion started by: migurus
2 Replies

6. Shell Programming and Scripting

move contents from one file to another based on line number or content

I want a script that will move everything beyond a certain line number or beyond a certain content word into another file. For example, if file A has this: first line second line third line forth line fifth line sixth line I want to run a script that will move everything beyond the third... (4 Replies)
Discussion started by: robp2175
4 Replies

7. Shell Programming and Scripting

Insert output into file at line number

I need to insert the output of a script into a specific line number of a txt file. I've read the Sed man page and searched the forums and it's not immediately clear how I would go about doing this. (4 Replies)
Discussion started by: pluto7777
4 Replies

8. Shell Programming and Scripting

Insert content of a file after a certain line in another file

Hi, it's my first post to this forum. I just started bash and I'm stuck at one issue. I want to include content of a file in another file after a certain line. I'm using sed for inserting one line but how to insert all content of a file ? For example i have a file list.txt with a few lines and... (4 Replies)
Discussion started by: ktm
4 Replies

9. Shell Programming and Scripting

extract content from a file and insert to another file

please help for the following task... I have to extract the mac address & IP address from the file1: ... 0100004512EEF4 03 192.168.0.7 192.168.0.1 -1 ... 0100779hF5D212 03 192.168.0.8 192.168.0.1 -1 ... 0100789lF5D212 03 192.168.0.9 192.168.0.1 -1 ... ... change the format (addidng... (15 Replies)
Discussion started by: fredao
15 Replies
Login or Register to Ask a Question