Insert a file text into another file after particular line
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Insert a file text into another file after particular line
I have a file 'install'.
I want to insert 8 to 18 lines of this file into another file 'space' after 17 lines.
I do not want to use shell script. I have done this already.
If you tell me using sed or head/tail in one line (So I can use it on terminal), it would be more appreciated.
1) If I want to insert complete file into another file at bottom, I use
$ cat 'file1' >> file2
2) If I want to insert particular text(suppose line number 7 to 20) of a file into another file at bottom, I use
$ tail -n +7 space | head -n 14 >> install
But what if I want to insert line number 7 to 20 of a file into another file at line number 8?
Please respond.
File 'space' has more than 500 lines.
I want to copy line number 7 to line number 20 of 'space' into file 'install' after line number 7.
It means line number 8 to 21 of install will be same as line number 7 to 20 of file 'space'. After that line number 22 of install will be same as old install's line number 8, line number 23 of install will be same as old install's line number 9 and so on.
If you are not getting this. I will tell you an example in next comment (if you want)
The green part represents the line to insert, the blue part are the lines to be inserted.
An example run on two small files:
Code:
$ awk '{ print } NR==8 { while (( "sed -n '3,6p' file_two" | getline ) > 0 ) print }' file_one
1 - one
2 - one
3 - one
4 - one
5 - one
6 - one
7 - one
8 - one
3 - two
4 - two
5 - two
6 - two
9 - one
$ cat file_one
1 - one
2 - one
3 - one
4 - one
5 - one
6 - one
7 - one
8 - one
9 - one
$ cat file_two
1 - two
2 - two
3 - two
4 - two
5 - two
6 - two
7 - two
8 - two
9 - two
@druuna
Thanks, this looks correct output.
But can we not do this using sed command only?
OP wants to do this using sed command.
The OP did mention sed/head/tail, but experience learns that people ask about the commands they have tried (and failed) with. They tend to "ignore" the other/better solutions, mostly due to inexperience with those commands.
It might be possible to create a complicated sed script that can do this, but a script isn't wanted by the OP.
Sorry but I do not want to use `shell script` as well as `awk` ( sorry I did not mention this).
I have given you example of head/tail. I want something similar like this.
Can't we do this using head & tail and >> ?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.