Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Concatenate text files with Windows command line, dropping leading lines

People also ask

How do I concatenate text files in Windows?

Find the document you want to merge. You have the option of merging the selected document into the currently open document or merging the two documents into a new document. To choose the merge option, click the arrow next to the Merge button and select the desired merge option. Once complete, the files are merged.

What command is best for concatenating two files together?

The cat (short for “concatenate”) command is one of the most commonly used commands in Linux as well as other UNIX-like operating systems, used to concatenate files and print on the standard output.


more +2 file2.txt > temp
type temp file1.txt > out.txt

or you can use copy. See copy /? for more.

copy /b temp+file1.txt  out.txt

I use this, and it works well for me:

TYPE \\Server\Share\Folder\*.csv >> C:\Folder\ConcatenatedFile.csv

Of course, before every run, you have to DELETE C:\Folder\ConcatenatedFile.csv

The only issue is that if all files have headers, then it will be repeated in all files.


I don't have enough reputation points to comment on the recommendation to use *.csv >> ConcatenatedFile.csv, but I can add a warning:

If you create ConcatenatedFile.csv file in the same directory that you are using for concatenation it will be added to itself.


Use the FOR command to echo a file line by line, and with the 'skip' option to miss a number of starting lines...

FOR /F "skip=1" %i in (file2.txt) do @echo %i

You could redirect the output of a batch file, containing something like...

FOR /F %%i in (file1.txt) do @echo %%i
FOR /F "skip=1" %%i in (file2.txt) do @echo %%i

Note the double % when a FOR variable is used within a batch file.


Here's how to do this:

(type file1.txt && more +1 file2.txt) > out.txt

I would put this in a comment to ghostdog74, except my rep is too low, so here goes.

more +2 file2.txt > temp
This code will actually ignore rows 1 and 2 of the file. OP wants to keep all rows from the first file (to maintain the header row), and then exclude the first row (presumably the same header row) on the second file, so to exclude only the header row OP should use more +1.

type temp file1.txt > out.txt

It is unclear what order results from this code. Is temp appended to file1.txt (as desired), or is file1.txt appended to temp (undesired as the header row would be buried in the middle of the resulting file).

In addition, these operations take a REALLY LONG TIME with large files (e.g. 300MB)