LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 02-13-2013, 03:42 AM   #1
bloodstreetboy
Member
 
Registered: May 2012
Posts: 201
Blog Entries: 3

Rep: Reputation: 37
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.
 
Old 02-13-2013, 03:54 AM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Awk could do that:
Code:
~$ awk 'NR>=8 && NR<=18' install

OR, To append it's output at the end of 'space'
~$ awk 'NR>=8 && NR<=18' install >> space
But let's know how many lines space file already have?

Last edited by shivaa; 02-13-2013 at 03:56 AM.
 
Old 02-13-2013, 04:12 AM   #3
bloodstreetboy
Member
 
Registered: May 2012
Posts: 201

Original Poster
Blog Entries: 3

Rep: Reputation: 37
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)
 
Old 02-13-2013, 04:18 AM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Check the "ed" editor. Stupidly simple.

You can even use diff to compair two files and let it generate the necessary commands.
 
Old 02-13-2013, 04:23 AM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Have a look at this one-liner:
Code:
awk '{ print } NR==7 { while (( "sed -n '7,20p' space" | getline ) > 0 ) print }' install
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
 
1 members found this post helpful.
Old 02-13-2013, 04:36 AM   #6
fortran
Member
 
Registered: Nov 2011
Location: Cairo, Egypt
Distribution: CentOS, RHEL, Fedora
Posts: 300
Blog Entries: 2

Rep: Reputation: 51
@druuna
Thanks, this looks correct output.
But can we not do this using sed command only?
OP wants to do this using sed command.

Last edited by fortran; 02-13-2013 at 04:41 AM.
 
Old 02-13-2013, 04:48 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by pavi_kanetkar View Post
@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.
 
Old 02-13-2013, 04:50 AM   #8
fortran
Member
 
Registered: Nov 2011
Location: Cairo, Egypt
Distribution: CentOS, RHEL, Fedora
Posts: 300
Blog Entries: 2

Rep: Reputation: 51
Great... Thanks Druuna !!!
 
Old 02-13-2013, 05:06 AM   #9
bloodstreetboy
Member
 
Registered: May 2012
Posts: 201

Original Poster
Blog Entries: 3

Rep: Reputation: 37
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 >> ?
 
Old 02-13-2013, 05:25 AM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Quote:
Originally Posted by bloodstreetboy View Post
Sorry but I do not want to use `shell script` as well as `awk` ( sorry I did not mention this).
sed, head, tail, awk, etc are all normal commands (which can also be used in a full blown script). Why the limitation?
Quote:
I have given you example of head/tail. I want something similar like this.
Can't we do this using head & tail and >> ?
Probably. But it won't be pretty, will be "unreadable" and will use multiple temporary files. In other words: messy.see post #12

You mention that you already have a script that does what you want/need, why not use that from the terminal?

Last edited by druuna; 02-13-2013 at 07:02 AM. Reason: Not messy after all.....
 
Old 02-13-2013, 05:32 AM   #11
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Using sed and cat:
Code:
sed -n '1,8p' file_one > /tmp/x.1 ; sed -n '9,$p' file_one > /tmp/x.2 ; sed -n '3,6p' file_two > /tmp/y.1 ; cat /tmp/{x.1,y.1,x.2} > file_new
file_new now contains:
Code:
$ cat file_new
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
 
Old 02-13-2013, 06:01 AM   #12
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by bloodstreetboy View Post
But what if I want to insert line number 7 to 20 of a file into another file at line number 8?
Code:
sed -i 8r<(sed -n 7,20p infile) oufile
 
1 members found this post helpful.
Old 02-13-2013, 08:15 AM   #13
bloodstreetboy
Member
 
Registered: May 2012
Posts: 201

Original Poster
Blog Entries: 3

Rep: Reputation: 37
Thanks colucix... I was asking something like this.

If I use 8r, it starts inserting from line number 9, I want it from line number 8 so I changed it to 7r.

Perfect.

Thank you..
 
Old 02-13-2013, 08:20 AM   #14
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Well done. You're welcome!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
read line by line a text file and define a file from each file yasmine Linux - Newbie 5 12-10-2012 04:53 AM
[SOLVED] trying to insert text in the last line of a file with sed command.... Sayan Acharjee Linux - General 7 10-04-2010 05:00 AM
bad: Want to insert a line into a text file using "sed" command eliote Linux - General 7 09-19-2010 02:55 AM
insert text to a file using command line replica88 Linux - Newbie 4 01-28-2010 05:50 PM
using sed to insert line into file and overwrite the current file jadeddog Programming 3 06-11-2009 07:14 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 04:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration