How to batch extract specified lines from multiple TXT files and merge them into one file
When multiple TXT Notepad documents only need to retain the beginning, end, odd-numbered lines, even-numbered lines, or other specified lines, opening and copying them one by one is not only time-consuming but also prone to missing selections or disrupting the order. This article uses HeSoft Doc Batch Tool as an example to introduce how to import multiple txt text files at once, uniformly set the extraction line range, and merge the extraction results into a single independent merge.txt file. The entire process is completed through a visual wizard, making it suitable for office scenarios such as log organization, data aggregation, material excerpting, and batch summary generation.
When a folder contains dozens or even hundreds of TXT Notepad documents, and you only need certain lines from each file, the traditional approach is usually to open each file one by one, locate the target lines, copy the content, and then paste it into a new text file. As the number of files grows, this method not only involves obvious repetitive work, but is also prone to problems such as copying one line too few, pasting in the wrong order, or missing a file entirely.
This article addresses exactly this kind of batch text organization need: using the office software HeSoft Doc Batch Tool , you can import multiple txt files at once, extract the corresponding lines according to a unified rule, and then merge all extracted results into a single TXT file. Taking the task in the screenshot as an example, we will extract the first 5 lines from each of 5 Notepad text files, and finally obtain a summary file named merge.txt. The entire process does not require opening the documents one by one, and is suitable for users who need to process files in batches, reduce mechanical operations, and improve the efficiency of data organization.
1. In which scenarios is batch extraction of specified TXT lines applicable?
"Extract lines from text files and merge them into one file" is not simply TXT concatenation. Ordinary merging usually retains all content from each source file, while this method can first filter by line and then merge them uniformly, so it is more suitable for office tasks that require only part of the information.
- Batch organizing log files: Extract the first few lines and last few lines from multiple daily log txt files to quickly summarize running status, title information, or error summaries.
- Collecting data at fixed positions: When multiple text files share the same structure, you can uniformly extract content at specified positions, avoiding opening them one by one and copying manually.
- Generating text summaries: Keep the first several lines from each document and merge them into one overview file for easier centralized reading and retrieval.
- Filtering odd or even lines: When the original text is saved in groups of two lines, or when valid data and description text are arranged alternately, you can filter by odd lines or even lines in batches.
- Consolidating collected results: Extract txt files exported from multiple programs, devices, or business systems according to unified rules, and save them together in one file.
- Batch processing research materials: For example, extract fixed ranges of paragraphs from multiple topic texts to form consolidated material needed for subsequent analysis.
If there are only one or two files, manual copying can also complete the task; however, as the number of files continues to increase, the batch processing advantage of office software becomes more obvious. You only need to set the rules once, and subsequent files can be processed under the same conditions, effectively reducing repetitive work.
2. Preview of the result: generating one summary file from multiple Notepad documents
1. Before processing: multiple independent TXT text files
Before processing, the folder contains multiple independent Notepad text files, including big_bang.txt, black_holes.txt, dark_energy.txt, dark_matter.txt, and galaxies.txt. These source files are all in txt format, and each file has its own text content.

If a manual method is used, you would need to open these 5 files one by one, select the target lines from each file, and then copy them into a new document. In actual work there may be far more than 5 files, so manual operation quickly increases the time cost. Before starting, it is recommended to confirm that these files indeed use the same or similar content structure so that a unified line extraction rule can be applied.
2. After processing: obtaining an independent merge.txt file
After the batch task is completed, the software writes the extracted text content into a new TXT file. The output result in the screenshot is merge.txt, which is a summary document independent of the original files.

The key point of this processing method is "extract first, then merge": the source files are not simply concatenated in full, but the required lines are first obtained according to the set range, and then a unified file is formed. The original multiple txt files can still be retained as source materials, while the newly generated merge.txt can be used for viewing, searching, sending, archiving, or further editing.
3. Operation steps: batch extract lines from multiple TXT files and merge them
Step 1: Enter the text tool and select the corresponding function
Open HeSoft Doc Batch Tool . This is a desktop office software for batch processing of office files, and the left side of the interface is divided into different tool entries according to file type and task purpose. Since the objects to be processed this time are Notepad txt files, first enter "Text Tools" on the left.
After entering the Text Tools page, select the "Merge & Split" category at the top. The page will display multiple functions related to text merging, folder merging, and text splitting. What is needed this time is not to merge all text directly in full, but to first extract some lines from each file, so you should select "Extract lines from text files and merge them into one file".

The name of this function already explains its processing logic: batch read multiple text files, extract the corresponding lines according to the settings, and then merge the results into one text file. Choosing the correct function is very important. If you mistakenly select "Merge text files into one file", you may get the full merged content of the source files instead of the filtered result of specified lines.
Step 2: Batch add the TXT files to be processed
After entering the function page, first come to the "Select records to be processed" section. The upper right area of the interface provides two import methods: "Add Files" and "Import Files from Folder".
- When the files to be processed are scattered in different directories, or when you only want to select a few of them, you can use "Add Files".
- When many txt files are concentrated in the same folder, you can use "Import Files from Folder" to reduce the operation of selecting them one by one.
In the screenshot, 5 txt files have already been imported. The list displays information such as file name, path, extension, creation time, and modification time, and the "Record Count" at the bottom shows 5. Users can check here whether the files are complete and whether any documents that do not need to be processed have been included by mistake.

If there are mistakenly selected files in the list, you can use the delete operation on the right side of the corresponding record to remove them; if the entire batch of files is wrong, you can also use "Clear" in the interface to import again. For a large number of files, you can also use the "Filter" and "Sort" functions displayed on the page to organize the list. After confirming that the files are correct, click "Next" at the bottom to enter the processing rule settings.
Expected result of this step: All txt files from which content needs to be extracted are present in the task list, and the file count and paths match the actual requirements. The extraction rules set later will be applied uniformly to these records.
Step 3: Set which lines to extract from each text file
After entering "Set Processing Options", the software displays "Processing Range". From the screenshot, the selectable ranges include "All lines", "First few lines", "Last few lines", "Odd lines", "Even lines", and "Custom". You should choose the corresponding method according to the text structure and the final need.

In the example, "First few lines" is selected, and the number 5 is filled in the "Range" field. This means that the software will extract the first 5 lines from each imported txt file, and then merge the extracted content from all files into one output file. The number 5 here is a unified rule, not a total of only 5 lines extracted.
Different processing ranges can correspond to different needs:
- All lines: Keep all lines from each source text, suitable for cases where a complete summary is required.
- First few lines: Suitable for extracting file titles, descriptions, summaries, time information, or header information. After selecting it, fill in the number of lines to keep in the range field.
- Last few lines: Suitable for extracting log endings, execution results, summaries, or trailing statistical information.
- Odd lines: Suitable for text structures where valid content consistently appears on lines 1, 3, 5, and so on.
- Even lines: Suitable for cases where content on lines 2, 4, 6, and so on should be retained.
- Custom: When the target content is not in consecutive beginning or ending lines, you can select this method and then complete the settings according to the rules provided in the interface.
When setting this, special attention should be paid to the concept of "line". In a text editor, each line break usually creates a new line, and blank lines may also occupy line numbers. If there are blank lines in the source files, it is recommended to open one or two sample files first to check the actual line structure, so as to avoid deviations between the extraction result and expectations.
Expected result of this step: The software clearly knows which lines need to be retained from each source file. In the example, the first 5 lines will be extracted from each txt file. After completing the settings, click "Next".
Step 4: Set the save location for the output file
From the process navigation at the top of the page, the complete task includes "Select records to be processed", "Set processing options", "Set save location", and "Start processing". After completing the line range settings, the next step is to choose the save location for the result file.
It is recommended to save the output file to a directory that is easy to find, and to keep it separate from the source files as much as possible. This preserves the original materials and also makes it easier to quickly identify the merged result. If a file with the same name already exists in the save directory, you should first confirm whether the old version needs to be retained to avoid affecting existing results.
For naming, you can use merge.txt, or use a clearer name based on the business content, such as "Log Summary.txt", "Project Materials Specified Lines Merge.txt", or "First 5 Lines Summary.txt". A clear file name is helpful for later searching and archiving. The actual options available on the save location page shall prevail.
Expected result of this step: Determine the output directory and related save settings for the new file, so that the generated result is clearly distinguished from the original txt files.
Step 5: Start batch processing and check the result
After completing the save location settings, enter the "Start processing" stage. Before executing the task, you can confirm three key pieces of information again: whether the number of imported files is correct, whether the selected processing range is correct, and whether the result save location is correct. After confirmation, start the processing and wait for the software to complete the batch extraction and merging.
After the task is finished, open the output directory and find the newly generated txt file. The example result is merge.txt. It is recommended to open the result file with Notepad, Notepad++, or another text editor, and focus on checking:
- Whether it contains the extracted content corresponding to all source files;
- Whether the number of lines actually extracted from each file matches the settings;
- Whether the content order meets the requirements for subsequent use;
- Whether Chinese characters, numbers, and special symbols display normally;
- Whether the original text files are still completely retained.
After confirming that everything is correct, the merged result can be used for centralized retrieval, data organization, content review, or archiving. Compared with manually copying file by file, batch processing can compress many repeated operations into one import, one configuration, and one execution.
4. Common questions and precautions
1. Why does the result appear to contain fewer than 5 items after selecting "First 5 lines"?
Blank lines in the text may also be counted as lines. If there are blank lines at the beginning of the file, the software may have completed the extraction according to the line count, but the visible valid text may be fewer than 5 items. It is recommended to first view the source file in Notepad to check whether it contains blank lines and confirm the line break structure.
2. Different TXT files have different numbers of lines. Can they be processed together?
They can be added to the same task, but you should choose a unified rule suitable for all files. For example, if some files have fewer than 5 lines, the content that can actually be extracted will be limited by the existing number of lines in the source file. To ensure consistent results, it is best to spot-check the shortest few files before processing.
3. How should I set it up if I want to extract the last few lines of each file?
In "Processing Range", select "Last few lines", and then fill in the range as required by the page. This method is suitable for centrally obtaining log ending status, final conclusions, or trailing statistical content, without manually scrolling to the end of each file.
4. What should I do if I only want to keep alternating data lines?
If the valid content in the source text is consistently distributed on lines 1, 3, and 5, you can select "Odd lines"; if it is distributed on lines 2, 4, and 6, you can select "Even lines". Before formal processing, confirm that the text structure is indeed consistent; otherwise, different files may produce different types of extracted content.
5. What should be noted about the file merge order?
After importing, check the file arrangement in the task list and use the sorting function provided in the interface to organize it. Especially when file names contain numbers, pay attention to whether the naming rules affect the order. For example, 1, 2, 10 and 01, 02, 10 may sort differently. If there are strict requirements on content order, it is recommended to complete the check before execution.
6. Is it necessary to back up the source files before batch processing?
From the example result, the task generates an independent merge.txt file. Even so, it is still recommended to keep backups of important business materials before batch operations and save the output file to a separate directory. This makes it easier to compare with the original text and to re-execute the task if the rules need to be adjusted.
7. How should I troubleshoot garbled Chinese characters?
TXT files may use different text encodings. If the source files themselves use inconsistent encodings, Chinese characters may display abnormally after merging. It is recommended to first spot-check whether the source files can be opened normally, and try to keep files in the same batch in a consistent encoding format. The screenshots in this article do not show encoding conversion options, so do not assume without confirmation that the software will automatically unify all encodings.
8. What is the difference between TXT and Word documents?
The objects processed in this article are txt plain text files, that is, common Notepad documents. doc and docx belong to the Word document format, and their internal structure is different from TXT. They cannot be treated as text files simply by changing the extension. If you need to batch process Word, docx, or doc files, you should enter the Word tools in the software and select a function that matches the actual task.
5. Summary
Batch extracting specified lines from multiple TXT files and merging them into one file is suitable for scenarios such as log summaries, fixed-field collection, text segment consolidation, and batch material organization. When using HeSoft Doc Batch Tool , the operation process can be summarized as follows: enter "Text Tools" and "Merge & Split", select "Extract lines from text files and merge them into one file", batch add txt records, set all lines, first few lines, last few lines, odd lines, even lines, or a custom range, then set the save location and start processing.
In the example, 5 Notepad documents were imported at once, the first 5 lines of each file were extracted uniformly, and an independent merge.txt was finally generated. Compared with opening and copying files one by one, this office software batch processing method can significantly reduce repeated clicking and manual checking, and is more suitable for tasks with a large number of files.
If you are currently facing a batch of similarly structured txt files, you can first select a small number of samples for testing, confirm that the extraction range and merge order are correct, and then import all files for batch execution. This can improve processing efficiency and reduce the probability of rework caused by improper rule settings.