Concatenate text files with Windows command line, dropping leading lines

前端 未结 12 860
心在旅途
心在旅途 2020-11-30 18:03

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.

相关标签:
12条回答
  • 2020-11-30 18:39

    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.

    0 讨论(0)
  • 2020-11-30 18:42

    The help for copy explains that wildcards can be used to concatenate multiple files into one.

    For example, to copy all .txt files in the current folder that start with "abc" into a single file named xyz.txt:

    copy abc*.txt xyz.txt
    
    0 讨论(0)
  • 2020-11-30 18:42
    more +2 file1.txt > type > out.txt && type file2.txt > out.txt
    
    0 讨论(0)
  • 2020-11-30 18:43

    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)

    0 讨论(0)
  • 2020-11-30 18:45

    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
    
    0 讨论(0)
  • 2020-11-30 18:46

    I know you said that you couldn't install any software, but I'm not sure how tight that restriction is. Anyway, I had the same issue (trying to concatenate two files with presumably the same headers) and I thought I'd provide an alternative answer for others who arrive at this page, since it worked just great for me.

    After trying a whole bunch of commands in windows and being severely frustrated, and also trying all sorts of graphical editors that promised to be able to open large files, but then couldn't, I finally got back to my Linux roots and opened my Cygwin prompt. Two commands:

    cp file1.csv out.csv
    tail -n+2 file2.csv >> out.csv
    

    For file1.csv 800MB and file2.csv 400MB, those two commands took under 5 seconds on my machine. In a Cygwin prompt, no less. I thought Linux commands were supposed to be slow in Cygwin but that approach took far less effort and was way easier than any windows approach I could find.

    0 讨论(0)
提交回复
热议问题