tags:

views:

666

answers:

6

I have two text files. I'd like to take the content of file1.txt that has four lines and append on the first four lines of the file2.txt. That has to be done overwriting all the records of the first four lines of file2.txt but keeping the rest of the original content (the other lines).

How can I do that using a batch or the windows prompt?

+1  A: 

Unless the four lines at the start of the two files occupy exactly the same amount of space, you can't, without rewriting the whole file.

You can't insert or delete data into files at arbitrary points - you can overwrite existing data (byte for byte), truncate the file or append to the end, but not remove or insert into the middle.

So basically you'd need to:

  • Start a new file consisting of the first four lines of file1.txt
  • Skip past the first four lines of file2.txt
  • Append the rest of file1.txt to the new file2.txt

You can do this fairly easily with the head/tail commands from Unix, which you could get from Cygwin if that's an acceptable solution. It's likely that the head/tail from the Windows Services for Unix would work too.

Jon Skeet
OK so the question is how do I delete the first four lines the file2.txt and append it on file1.txt? (Using a batch) Thank you!
Your question is a bit confusing. Please give an example with two sample files, before and after.
Jon Skeet
+1  A: 

With batch alone I'm not sure you can do it.

With Unix commands you can -- and you can easily use Unix commands under Windows using Cygwin.

In that case you want:

#!/bin/bash
head -n 4 file1.txt > result.txt   # first 4 lines of file1
tail -n +5 file2.txt >> result.txt # append lines 5, 6, 7... of file2
mv result.txt file2.txt    # replace file2.txt with the result
Jason Cohen
Should that be tail -n +5 for lines 5 to end?
Andy
yes thanks! fixed now
Jason Cohen
A: 

you could do it if you wrote a script in something other than windows batch. vbscript or jscript with windows scripting host should be able to do it. Each of those would have a method to grab lines from one file and overwrite the lines of another.

Jeremy Wall
A: 

You can do this by creating a temporary third file, pulling the lines from the first file and adding them to the temp file, then reading the second file and, after reading in four carriage return/linefeed pairs, write the rest to the temp file. Then, delete the second file and rename the temp file to the second file name.

Michael Todd
+1  A: 

If you grab the coreutils from Gnutils you'll be able to do a lot of the stuff you can do with Cygwin without having to install cygwin. Then you can use things like head, tail and cat which will allow you to do what you're looking to.

e.g.

head -n 4 file2.txt

to get the first four lines of file2.

Extract the zip from the page linked above, and grab whichever of the utils you need to use out of the bin directory and put them in a directory in your path - e.g. for the below you'd want mv, head and tail. You could use the built in DOS move command, but you'd need to change the options slightly.

The question is a little unclear, but if you're looking to remove the first four lines of file2.txt and append them to file1.txt you can do the following:

head -n 4 file2.txt >> file1.txt
tail -n +5 file2.txt >> temp.txt
mv temp.txt file2.txt
Andy
+3  A: 
copy file1.txt temp.txt
echo. >> temp.txt
more +5 file2.txt >> temp.txt
move /y temp.txt file2.txt

EDIT: added the "echo. >> temp.txt" instruction, which should add a newline to temp.txt, thereby allowing for a "clean" merge of file2.txt (if file1.txt doesn't end with a newline).

Guido Domenici
Should that be more +4 ?
Andy
Hello, I'm almost there but the content of the 5th line is appending with the content on the four line. I'd like to keep the 5th line untouchable.
@Andy: I guess it should be +5 coz he wants to eliminate the first four lines of file2.txt (hence start copying from the 5th line).@fguser: I have edited the script by inserting the "echo." line - see if it does it for you.
Guido Domenici
And that's why I love SO. Great answer.
Jim Mischel
Perfect! Thanks a lot Guido!
nice command line foo :)
dr. evil
@Guido: I'm playing with more on XP and it seems to consider +0 to be start from line 1, so +5 is starting on line 6. Tail on *nix, on the other hand, does it the other way.
Andy