Concatenate text files with Windows command line,

2019-01-08 04:45发布

I need to concatenate some relatively large text files, and would prefer to do this via the command line. Unfortunately I only have Windows, and cannot install new software.

type file1.txt file2.txt > out.txt

allows me to almost get what I want, but I don't want the 1st line of file2.txt to be included in out.txt.

I have noticed that more has the +n option to specify a starting line, but I haven't managed to combine these to get the result I want. I'm aware that this may not be possible in Windows, and I can always edit out.txt by hand to get rid of the line, but is there a simple way of doing it from the command line?

11条回答
霸刀☆藐视天下
2楼-- · 2019-01-08 04:54

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.

查看更多
贼婆χ
3楼-- · 2019-01-08 04:57

In powershell:

Get-Content file1.txt | Out-File out.txt
Get-Content file2.txt | Select-Object -Skip 1 | Out-File -Append out.txt
查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-01-08 04:57

You can also simply try this

type file2.txt >> file1.txt

It will append the content of file2.txt at the end of file1.txt

If you need original file1.txt, take a backup beforehand. Or you can do this

type file1.txt > out.txt
type file2.txt >> out.txt

If you want to have a line break at the end of the first file, you can try the following command before appending.

type file1.txt > out.txt
printf "\n" >> out.txt
type file2.txt >> out.txt
查看更多
成全新的幸福
5楼-- · 2019-01-08 04:57
more +2 file1.txt > type > out.txt && type file2.txt > out.txt
查看更多
Juvenile、少年°
6楼-- · 2019-01-08 05:01

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)

查看更多
冷血范
7楼-- · 2019-01-08 05:01

Here's how to do this:

(type file1.txt && more +1 file2.txt) > out.txt
查看更多
登录 后发表回答