Joining many files into one with TextPipe
Data often arrives in pieces. A reporting system writes one file per branch, a logger starts a new file every hour, or an export tool splits its output into 2 GB parts. Before the data can be loaded or analysed, the pieces have to be put back together. The usual tools for this are the Windows copy command and the Unix cat command. Both concatenate bytes and nothing else, so every problem in the source files is carried into the result: repeated header rows, a last line with no line break, or records that are glued together on one line.
TextPipe joins files as part of a filter set, so you can clean up each file while it is being added. You can select a whole folder tree, sort the file list by name or date, and write a single output file. Because the processing is streamed, the combined file can be far larger than available memory. Files can be text or binary. For binary files such as fixed-length mainframe records, no characters are altered during the join.
Sample input and output
Three monthly sales files each begin with the same header line:
jan.csv: Region,Units,Revenue feb.csv: Region,Units,Revenue
North,120,4800 North,98,3920
South,75,3000 South,110,4400
A plain concatenation repeats the header in the middle of the data. Joining with TextPipe, keeping the header from the first file only and adding the source file name as a new column, gives:
Region,Units,Revenue,Source
North,120,4800,jan.csv
South,75,3000,jan.csv
North,98,3920,feb.csv
South,110,4400,feb.csv
The result can be loaded into a database table or opened in Excel without any further edits. Adding the source file name also keeps an audit trail of where each record came from.
Typical settings
- File order: by name, by modified date, or by the order in which you list them. Date-stamped names such as 2024-01-31.log sort correctly by name.
- Header handling: keep the first line of the first file only, or skip a fixed number of lines in every later file.
- Separator: insert a line break, or a marker line containing the file name, between files that may not end in a newline.
- Output size: write one file, or start a new output file after a chosen size so that results stay below the limit of the target system.
Practical applications
Finance teams combine branch extracts into a single ledger file. System administrators merge rotated log files before a month-end analysis. Publishers join chapter files into one manuscript. Researchers combine survey exports from different collection sites. In every case the same filter set can be saved and rerun when the next batch of files arrives.
Common pitfalls
- Files that use different line endings (Windows, Unix or old Mac) produce a mixed file. Normalise the line endings in the same filter set.
- Files saved in different encodings, for example UTF-16 and ANSI, cannot simply be concatenated. Convert each to one encoding first.
- A missing final line break joins the last record of one file to the first record of the next. Use a separator to avoid this.
Related pages
The join and merge files feature guide explains each option in detail. You may also want to sort the joined file or convert encodings before merging. The publishing case study and the data conversion solutions page show larger projects. For the same job on Word documents, see WordPipe.