Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

windows - Batch Combine CSV Remove Header

I have multiple CSV files with the same header and I'm trying to combine them together in Batch and keep only a single header. Any ideas?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could use MORE +1 to output all but the 1st line.

>new.csv (
   type file1.csv
   more +1 file2.csv
   more +1 file3.csv
   REM etc.
)

Obviously you can adjust the number of lines to skip in each file as needed.

To combine all csv files in the current folder: Edit: modified to not use newly created output csv as input

@echo off
setlocal
set first=1
>new.csv.tmp (
  for %%F in (*.csv) do (
    if defined first (
      type "%%F"
      set "first="
    ) else more +1 "%%F"
  )
)
ren new.csv.tmp new.csv

Obviously this is only effective if all the csv files share the same format.

EDIT 2015-07-30: There are some limitations:

  • Tab characters will be converted into a string of spaces
  • Each CSV source file must have fewer than 64k lines

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...