How to batch extract specified lines from multiple TXT files and merge them into one text file
Translation:EnglishFrançaisDeutschEspañol日本語한êµì–´ï¼ŒUpdate Time:2026-11-11 15:15:12
When a folder contains a large number of TXT Notepad documents, and you only need the first few lines, last few lines, odd lines, even lines, or other specified lines from each file, opening and copying them one by one is not only time-consuming but also prone to omissions. This article explains how to use the text batch processing feature in office software to import multiple txt files at once, uniformly set the line extraction range, and merge the extraction results into a single independent text file. The operation process requires no scripting, making it suitable for data compilation, log organization, text summary collection, and batch data preprocessing scenarios.
When faced with dozens or even hundreds of Notepad text files, if you only need certain lines from them, the traditional approach is usually to open each txt file one by one, locate the target content, copy it, and paste it into a new document. Once the number of files grows, this method is not only repetitive, but also prone to missed copies, duplicate pastes, and disordered sequences. This is especially true when organizing logs, consolidating text excerpts, collecting opening notes from files, or extracting data from fixed positions, where manual work can take up a great deal of time.
The problem this article aims to solve is: how to batch extract specified lines from many TXT text files, and then merge the extracted content into a single independent file. The following demonstration uses HeSoft Doc Batch Tool . It is a file batch processing application designed for everyday office scenarios. It can perform unified processing across multiple documents through a visual interface, reducing the workload of opening files one by one and repeatedly copying and pasting. The entire operation does not require writing commands or scripts; simply follow the wizard to import files, set the extraction range, and run the process.
1. Applicable scenarios: when do you need to batch extract specified lines from TXT files
The core value of batch extracting text lines and merging them is to centralize operations that would otherwise need to be repeated across multiple files. As long as the txt files to be processed have a similar structure, or you need to retrieve content from the same position in each file, this method is worth considering.
1. Consolidate information from the beginning of multiple text files
Some text materials place the title, number, date, source, and content summary in the first few lines of the file. If you only need this basic information, you can batch extract the first several lines of each TXT file and merge them into one file for easier unified reading, searching, or further organization.
2. Collect results from the end of log files
Program logs, inspection reports, or runtime records often place execution results, statistical data, and error information at the end of the document. In this case, you can choose to extract the last few lines, avoiding the need to review lengthy log files one by one.
3. Extract odd-numbered lines, even-numbered lines, or content at specific positions
Some regularized texts are arranged in an alternating format such as "one line for the field name, one line for the field value", or may store one data item every fixed number of lines. The software interface provides processing ranges such as odd-numbered lines, even-numbered lines, and custom settings, so you can choose the appropriate extraction method based on the original text structure.
4. Merge scattered Notepad materials
Meeting notes fragments, collection results, research materials, device-exported data, or content assets may be saved separately across multiple Notepad documents. If you only want to keep part of each file and then form a consolidated text, you do not need to trim each file individually and then merge them manually; extraction and consolidation can be completed in a single task.
5. Batch data preprocessing
Before importing text into Excel, a database, an analysis program, or another business system, it is often necessary to delete irrelevant lines and retain a fixed range. First batch extracting the target lines from multiple txt files into one file can reduce the subsequent workload of cleaning and formatting.
2. Result preview: generating one merged file from multiple TXT files
Before processing: multiple independent Notepad text files
The example folder contains 5 TXT files, named big_bang.txt, black_holes.txt, dark_energy.txt, dark_matter.txt, and galaxies.txt. They are all independent Notepad documents. If using a manual approach, you would need to open these 5 files one by one and then copy the required lines from each.

In actual work, the number of files may be far greater than in this example. Whether there are a few, dozens, or even more text files, the processing idea is the same: first add all files to be processed to the task list, then set one shared line extraction rule.
After processing: extraction results merged into a separate merge.txt
After batch processing is completed, the text lines matching the conditions from multiple source files are written together into a new text file. In this example, the result is merge.txt. The original 5 txt files still exist as input files, while the consolidated result is saved separately for convenient subsequent opening, editing, searching, or sharing.

As can be seen from the result, this approach is not simply viewing files one by one, but rather continuously completing both "extracting target lines from each file" and "merging the results into one file". For text materials with a consistent structure, this can significantly reduce the number of repetitive operations.
3. Preparation before operation
Before starting, it is recommended to first confirm the source files and extraction rules, so that you do not discover after completing the batch task that the range settings do not meet your needs.
- Confirm the file type: this tutorial processes TXT text files, which are common Notepad documents.
- Confirm the target lines: first determine whether you need all lines, the first several lines, the last several lines, odd-numbered lines, even-numbered lines, or another custom range.
- Check the file structure: if all files use the same structure, one set of rules can usually be applied directly in batch; if the target content in different files is located at different positions, it is recommended to process them separately by structure.
- Organize input files: you can place the documents to be processed into the same folder in advance. Later, using "Import files from folder" will be more convenient.
- Keep the original materials: for important files, keeping a backup before processing is the safer practice. It is also recommended to save the output file to an easily identifiable location to avoid confusion with the source files.
4. Steps for batch extracting text lines and merging them
Step 1: enter the text tool and select the corresponding function
Open HeSoft Doc Batch Tool , and in the left function bar, enter "Text Tools". At the top of the function page, select the "Merge and Split" category, then find and enter "Extract lines from text files and merge them into one file".

The purpose of this step is to choose the correct processing mode. The page also contains other tools such as text merging, folder merging, and text splitting. However, this task requires not only merging files, but also first extracting specified lines from each file, so you should choose "Extract lines from text files and merge them into one file". After entering it, the software will guide you through file selection, processing options, save location, and starting the process using a step-by-step wizard.
Step 2: batch import the TXT files to be processed
After entering the function page, you first arrive at "Select records to be processed". You can click "Add Files" at the top to select one or more txt files; if the files to be processed are already gathered in the same folder, you can also use "Import files from folder" to add them in batch.

In the example, 5 files have already been imported. The list shows information such as sequence number, name, path, extension, creation time, and modification time, and the summary area at the bottom displays "Record count: 5". After importing, you should focus on checking the following:
- whether the number of files matches the planned quantity;
- whether the extension is txt;
- whether the names and paths point to the correct source files;
- whether any documents that do not need to be processed have been mixed into the list.
If you find that a file was added by mistake, you can remove it from the current task using the delete action on the right side of the corresponding record; if you want to reselect, you can also use "Clear" at the top of the page. After confirming that the list is correct, click "Next" at the bottom to enter the processing option settings.
Step 3: set which lines to extract from each text file
On the "Set processing options" page, you need to select the processing range. According to the screenshot, the software provides options such as all lines, the first several lines, the last several lines, odd-numbered lines, even-numbered lines, and custom. You should choose based on the source file structure and final purpose, rather than applying the same rule by default.

In the example, "the first several lines" is selected, and 5 is entered in the "Range" field. This means the software will extract the first 5 lines from each imported text file and then consolidate these results into one file. Since 5 txt files were imported in the previous step, the software will apply the same extraction rule to every file in the list.
The processing ranges can be understood as follows:
- All lines: retain the entire text content of each source file, suitable for directly merging multiple txt files into one file.
- The first several lines: extract a specified number of lines from the beginning of each file, suitable for collecting titles, summaries, numbers, or header information.
- The last several lines: extract a specified number of lines from the end of each file, suitable for consolidating results, conclusions, or log tail information.
- Odd-numbered lines: extract lines at positions 1, 3, 5, and so on, suitable for texts whose content is arranged alternately by line.
- Even-numbered lines: extract lines at positions 2, 4, 6, and so on, also suitable for materials with a fixed alternating structure.
- Custom: used when the target lines do not fall into the above conventional ranges. For specific settings, fill them in according to the input requirements actually shown in the software interface.
After completing the settings, it is recommended to check the range value again. For example, if you need the first 5 lines, enter 5; if you need the last 10 lines, select "the last several lines" and enter the corresponding range. After confirming, click "Next".
Step 4: set the save location for the merged file
From the process indicator at the top of the page, you can see that after processing options comes "Set save location". After entering this step, follow the prompts on the software page to select the save location for the output result. It is recommended to save the merged file to a separate result folder, or to a location that is easy to distinguish from the source file directory.
The purpose of setting the save location is to specify where the newly generated consolidated text will be written. For easier later identification, you can give the result file a meaningful name, such as "first 5 lines summary.txt", "log tail result summary.txt", or "merge.txt". The file name should be based on the actual project naming and should not duplicate the original file names.
Step 5: start processing and check the generated result
After confirming the save location, enter the "Start processing" stage in the workflow. Before executing the task, you can perform a final check of the number of imported files, the line extraction method, the range value, and the output location, and then start processing after confirming everything is correct.
After processing is complete, go to the configured save directory to view the result file. In this example, the generated file is merge.txt. After opening the file, you can spot-check content at different positions to confirm that the target lines from each source file have been written, and also check whether any files are missing, whether the range is incorrect, or whether the content order differs from expectations.
If the check result does not meet expectations, you usually do not need to reorganize everything manually. Simply return to the task settings, adjust the line range or input file list, and process again. This is also an advantage of using office batch processing software: unified rules can be executed repeatedly, and changing the settings once is enough to reprocess the entire batch of files.
5. Common questions and precautions
1. What is the difference between selecting "all lines" and ordinary text merging
If you select "all lines", the actual effect is to retain the entire content of each file and output it centrally, which is suitable for fully merging text; if you only need part of each file, you should select the first several lines, the last several lines, odd-numbered lines, even-numbered lines, or a custom range. The focus of this function is that it can filter by line before merging, rather than requiring all content to be written into the result file.
2. What should be done if a file has fewer lines than the specified number
For example, if you set extraction of the first 5 lines, but a certain file actually has fewer than 5 lines, that file itself does not have enough lines to extract. Before batch processing, you should spot-check the text length, and after processing, you should also check the output content corresponding to short files to avoid mistaking "insufficient source file content" for a processing omission.
3. Why is the merged result inconsistent with the desired content
Common reasons include selecting the wrong processing range, entering an incorrect range value, inconsistent source file structures, or including other text in the import list. It is recommended to test with a small number of copies first, for example importing 2 to 3 files first, confirming that the extraction rule is correct, and then processing the complete folder.
4. Does file order affect the merged result
When multiple files are ultimately written into the same result, the order of the input records is usually a factor that needs attention. The software file list provides a "Sort" entry. If you have requirements for the order of the consolidated content, you should check the list arrangement before starting processing and organize it using the sorting function provided in the interface. After completion, spot-check the result again to confirm that the order of content from each file meets your usage needs.
5. Can TXT files with different structures be processed at once
Multiple files can be added to the same task, but the same batch task applies a unified extraction rule. If some files need the first 5 lines and others need the last 3 lines, it is recommended to split them into two separate tasks. Grouping by structure can reduce range setting errors and make it easier to verify the results.
6. Can the original files be deleted after processing is complete
It is not recommended to delete the source files before checking the merged result. You should first open the newly generated txt file, verify the completeness of the content and the extraction range, and keep a backup of the original materials. After confirming that the consolidated file can be used normally, handle the original files according to your organization's or personal file management practices.
7. How to troubleshoot when text appears garbled
TXT files from different sources may use different text encodings. If individual files already appear garbled when opened, you should first confirm whether the source file can be displayed normally in a common Notepad tool. For a batch of files from complex sources, it is recommended to perform sample checks first and try to ensure that the encoding and content state of the input text are consistent before performing batch extraction and merging.
8. How to reduce the probability of batch processing errors
It is recommended to use the approach of "small-scale testing - checking results - expanding the batch". First select a few representative files for a trial run to confirm that the settings for the first few lines, last few lines, or odd/even lines meet the requirements, and then import all materials. For important data, you should also store the source files and output files separately, and use clear folder names to record the processing batch.
6. Summary
When a large number of Notepad documents need content extracted from the same position, opening TXT files one by one and copying them manually is time-consuming and prone to omissions. With the "Extract lines from text files and merge them into one file" function of HeSoft Doc Batch Tool , you can import multiple txt files at once, uniformly select all lines, the first several lines, the last several lines, odd-numbered lines, even-numbered lines, or a custom range, and then save the extraction results as a separate text file.
This processing method is especially suitable for text excerpt consolidation, log result organization, fixed-line data extraction, and batch material preprocessing. It converts repetitive operations such as opening files, locating content, copying, and pasting into a reusable batch processing workflow, helping to reduce manual errors and improve office efficiency.
If you currently have a batch of structurally similar TXT files to organize, you can first copy a small number of samples for testing: import the files, set the target line range, select the save location, and run the process. After confirming that the results are correct, batch process the complete set of materials to more reliably complete the extraction and merging of specified lines from multiple text files.
Keyword:Batch extract specified lines from TXT , merge multiple text files , batch process Notepad files , merge TXT files , batch extract text lines