LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-19-2011, 10:31 AM   #1
freet
LQ Newbie
 
Registered: Apr 2011
Posts: 2

Rep: Reputation: 0
Script to copy paste text from one file to another (overwriting part of the file)


Hello,

basically I want to do what the topic title suggests
I have a .txt-file with ~50.000 lines of numbers, generated by a mathematics program. From this file, I need line ~ 1.100 to line ~16.000 (these lines are always the same btw, this may make the solution easier, dunno) to be copy/pasted to another file, where the lines ~500 to ~15.000 (also, every time the same) should be overwritten by the aforementioned lines...
I haven't found or come up with anything that works yet, mostly I find solutions to copy everything from one file to another but I can't find something to specifically overwrite a part of a file with part of another...

Thx in advance if anyone can help me!

Grts,
Freet
 
Old 04-19-2011, 11:58 AM   #2
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 & 7
Posts: 3,221

Rep: Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873
What you need to do is generate a new file which contains:

lines 1-499 of file 2
lines 1.100 to 16.000 of file 1
lines 15.101 to end of file 2

then if all that works, the new file replaces file 2, right?

Code:
head -n 499 file2 >newfile
head -n 16000 |tail -n 14900 >>newfile
LEN=`cat file2 |wc -l`
REST=$(($LEN - 15000))
tail -n $REST file2 >>newfile
 
Old 04-19-2011, 12:02 PM   #3
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,842

Rep: Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004Reputation: 2004
I don't see any simple way to do this with a single command, at least not in the shell. Directly replacing blocks of text in the middle of a file like this is not a trivial thing.

The logical solution is simply to use sed (or a similar tool) to extract and copy lines 0-499 from the first file into a temporary file, then take lines 11,000-16,000 from the second file and append it to the first batch, and finally append lines 15,001+ from the first file again. Then you can overwrite the original with the new file at the end.

Code:
sed -n '1,499 p' inputfileA >outputfile
sed -n '11000,16000 p' inputfileB >>outputfile
sed -n '15001,$ p' inputfileA >>outputfile
mv outputfile inputfileA
The > file redirector in the first command either creates a new file or overwrites an existing one. The >> in the following lines will append to an existing file. You can use >> in the first line too, if you're sure the output file doesn't already exist. $ in this case represents the last line of the file.

Using a temp file also allows you to protect the original data until you're sure the operation completed successfully.
 
1 members found this post helpful.
Old 04-19-2011, 01:47 PM   #4
smallpond
Senior Member
 
Registered: Feb 2011
Location: Massachusetts, USA
Distribution: CentOS 6 & 7
Posts: 3,221

Rep: Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873
The sed scripts in comment 3 are simpler than my use of head and tail in comment 2. I just don't know sed that well.

I wanted to point out that editors create a new file, they don't edit "in place" in the existing file. So there is not some more efficient way to do this.
 
Old 04-19-2011, 04:23 PM   #5
freet
LQ Newbie
 
Registered: Apr 2011
Posts: 2

Original Poster
Rep: Reputation: 0
Thank you very much both of you, this has solved my problem!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
[SOLVED] How to copy-paste part of line within the file? guessity Linux - Newbie 10 12-25-2009 02:25 AM
[SOLVED] Need help with script to replace certain text in file with part of the file's name kmkocot Linux - Newbie 2 08-23-2009 05:06 PM
Copy Text File and Paste to /hdd CD-RW aquatux SUSE / openSUSE 3 12-29-2006 07:32 AM
Which light text editor can copy text from file and paste in browser? davidas Linux - Software 9 03-06-2006 12:28 PM
Shell script to copy file name with part of directory Transition Linux - General 5 01-18-2005 06:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:18 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration