How to batch replace entire lines containing keywords in multiple TXT text files
Translation:EnglishFrançaisDeutschEspañol日本語한ęµě–´ďĽŚUpdate Time:2026-08-03 06:46:49
When multiple TXT text files contain content with the same structure, if you only want to replace entire lines containing certain keywords with new standard lines, manually opening and modifying each file is not only time-consuming but also prone to omissions. This article uses HeSoft Doc Batch Tool as an example to demonstrate how to batch import multiple TXT files, set a list of keywords to look for and the replacement content, and complete multi-file processing in one go through a wizard-style operation. It is suitable for batch standardization scenarios such as directories, configuration descriptions, log texts, and data lists.
In daily office work, data organization, system maintenance, or content archiving, a repetitive task often arises: a folder contains many TXT text files, and each file has several lines that need unified modification. For example, multiple text files all contain directory lines like “Annex A”, “Annex B”, “Annex C”, “Annex D”, and now these entire lines containing the keywords need to be replaced with a new standard description. If you open 1.txt, 2.txt, 3.txt, 4.txt, 5.txt one by one, then copy, paste, and save line by line, it becomes highly inefficient when there are many files, and problems like missed changes, wrong modifications, and inconsistent formatting are very likely to occur.
This article addresses the problem of "Bulk replacing all lines containing keywords in many text files with new lines." The office software used here is the one shown in the screenshot: " HeSoft Doc Batch Tool ". Its positioning is not just a plain text editor, but a batch file processing tool oriented towards office scenarios. Its core value lies in centralizing file operations that originally required repeated execution into a single workflow, thereby reducing manual operation time and improving processing accuracy.
Below, combined with before-and-after effect screenshots and software operation screenshots, we will detail how to batch replace complete lines containing specified keywords in multiple TXT text files.
Applicable Scenarios: Which situations are suitable for batch replacing entire lines containing keywords?
"Find and replace complete lines in text files based on keywords" is suitable for scenarios requiring line-by-line updates of text content. It differs from ordinary "find and replace a word"; the focus is not just replacing the keyword itself, but locating the entire line containing the keyword and then replacing it with a new complete line.
For example, a line in the original text is “Annex A Food and drink standards, and nutrient standards for primary and secondary aged pupils - revised 2020”, where “Annex A” is only the starting keyword of this line. If we want the whole line to become “Annex A - Q&A”, a normal string replacement is not direct enough, as it would only replace the characters “Annex A”, leaving the subsequent old description intact. In this case, using full-line replacement is more appropriate.
Common applicable scenarios include:
- Bulk modifying directory items, title lines, or description lines in multiple TXT text files.
- Bulk updating the content of entire lines containing a specific field in configuration files.
- Bulk cleaning old descriptions in logs, lists, and data export files.
- Unified replacement of old version entries with new version entries across multiple text documents.
- Standardizing English, Chinese, or mixed-language TXT files.
If your file extension is .txt and you are dealing with plain text content, this type of batch processing method is usually more stable than manual editing. For text files, instruction documents, export lists, batch configuration records, etc., in office scenarios, you can consider using this approach.
Effect Preview: Before processing, multiple TXT files contain old lines that need replacement
From the pre-processing file screenshot, you can see there are multiple text files in the folder: 1.txt, 2.txt, 3.txt, 4.txt, 5.txt. This means the objects to be processed are not a single file, but a group of TXT files with similar structures whose content needs unified updating.

After opening one of them, 1.txt, you can see multiple lines starting with “Annex A”, “Annex B”, “Annex C”, “Annex D” at the beginning of the text. These lines are followed by longer explanation text, such as “Food and drink standards...” and “The secondary school analysed meal”. From the red boxes and arrow annotations, it can be seen that the target for this operation is precisely these complete lines containing the specified keywords.

In actual work, similar content might be distributed across dozens or even hundreds of text files. Manually opening each file, finding “Annex A”, selecting the entire line to replace, then continuing with “Annex B”, “Annex C”, “Annex D” is not only cumbersome but also prone to fatigue errors due to similar file content. The value of using batch processing tools lies in delegating these repetitive find-and-replace actions to the software.
Effect Preview: After processing, lines containing keywords have been replaced with new content
The post-processing screenshot shows that the original lines starting with “Annex A”, “Annex B”, “Annex C”, “Annex D” and containing long descriptions have been replaced with new, shorter standard lines:
- Annex A - Q&A
- Annex B - Safety Attentions
- Annex C - Important Notes
- Annex D - Other Remarks

It should be noted that “Contents” and the subsequent body text in the screenshot are still preserved. This indicates that this process is neither about deleting the entire file content nor indiscriminately overwriting all text, but only about locating lines containing the specified keywords and replacing those lines with the corresponding new lines. For office scenarios where other file content needs to be preserved and only some lines updated, this processing method is more precise.
Operation Steps: Using HeSoft Doc Batch Tool to Batch Replace Whole TXT Lines
Below, following the operational sequence in the screenshots, the complete processing workflow is introduced. As the software uses a wizard-style process, users only need to follow the steps to select functions, import files, set keywords and replacement content, then proceed to set the save location and start processing.
Step 1: Enter Text Tools and select the full-line replacement function
After opening " HeSoft Doc Batch Tool ", select "Text Tools" from the left function category. On the Text Tools page, you can see multiple batch functions related to text files, such as Find and Replace Keywords in Text, Delete Whitespace in Text, Convert Text to Word, Convert Text to PDF, etc.
The goal this time is "as long as a line contains a specified keyword, replace that entire line with new text," so you need to choose the function highlighted in the screenshot: "Find and replace complete lines in text files based on keywords." The description of this function also indicates it is used for "batch deleting or replacing entire lines containing a certain keyword in text files with new text."

The purpose of this step is to enter the correct processing module. After selecting this function, the subsequent interface will revolve around text files, keyword searching, and whole-line replacement, rather than ordinary partial string replacement.
Step 2: Add or import the TXT files that need processing
After entering the function page, the software shows Step 1 "Select records to be processed". The top provides buttons like "Add File", "Import Files from Folder", "Clear", "More". For cases with only a few files, you can click "Add File" to select them one by one; if you need to process multiple text files in a folder, you can use "Import Files from Folder".
The screenshot shows that 5 txt files have been imported, named 1.txt, 2.txt, 3.txt, 4.txt, 5.txt, with the path located in the D drive test folder. The table also displays information like extension, creation time, and modification time, making it easy to confirm if the files are correct before execution.

The expected result of this step is: all TXT files needing batch processing appear in the list, and the bottom summary shows the record count as 5. If you find files that do not need processing have been imported, you can remove them using the delete button in the operation column; if the import list is incorrect, you can also use "Clear" and re-add. After confirming the file list is correct, click "Next" at the bottom of the interface.
Step 3: Set the search method and enter the list of keywords to find
After entering Step 2 "Set processing options", you first need to set the keyword search method. The screenshot shows "Exact text search" is selected, and you can also see the option "Use formula for fuzzy text search". In this example, the keywords to match are definite text strings like “Annex A”, “Annex B”, “Annex C”, “Annex D”, so using exact text search is more intuitive.
In the "Additional Options" area, you can also see options like "Ignore letter case" and "Match whole word only (not part of a word)". Whether to check these depends on the actual file content. If your files have inconsistent case, e.g., both "annex a" and "Annex A" exist, you can pay attention to case-related settings; if you worry the keyword might just be part of a longer word, you can combine this with the whole word matching option for judgment. The screenshot example mainly shows exact keyword matching.
Next, enter the keywords to be located line by line in the left "List of keywords to find". In this example, 4 keywords were entered:
- Annex A
- Annex B
- Annex C
- Annex D

The key point here is: the left list is not the complete old line to be replaced, but the keyword used to locate the target line. As long as a line contains the corresponding keyword, the software will identify that line as one needing processing.
Step 4: Fill in the new line content for replacement, ensuring one-to-one correspondence
In the right "List of replacement keywords", you need to fill in the new line content after replacement. The screenshot shows four new content lines corresponding to the four keywords on the left:
- Annex A - Q&A
- Annex B - Safety Attentions
- Annex C - Important Notes
- Annex D - Other Remarks
The purpose of this step is to establish the correspondence between "found keyword" and "replacement new line". That is, when the software finds the entire line containing “Annex A” in a text file, it will replace this whole line with “Annex A - Q&A”; when it finds the line containing “Annex B”, it replaces it with “Annex B - Safety Attentions”, and so on.
When entering, it is recommended to check line by line to ensure the order on both sides is consistent. Because full-line replacement usually directly alters file content, if the left keyword and right new content are misaligned, problems like “Annex A” being replaced with the description for “Annex B” can occur. For batch file processing, pre-execution verification is very important.
Step 5: Proceed to the save location setting and start processing
After setting the keywords and replacement content, click "Next" at the bottom. According to the top process flow in the interface, you will then enter Step 3 "Set save location", and then Step 4 "Start processing". Although the screenshot does not expand the save location page, it is clear from the wizard steps that the software will let the user set output or save-related information before formal execution.
At this step, it is advisable to choose a suitable save location based on actual work habits, and confirm that the file list, keyword list, and replacement content have been checked correctly before starting processing. After completing the save location setting, enter "Start processing", and the software will batch execute the search and full-line replacement on the multiple txt files in the list.
After the processing is complete, you can open one of the files for a spot check. Comparing the screenshots before and after, you can see that the old lines containing “Annex A”, “Annex B”, “Annex C”, “Annex D” have been replaced with the new standard lines, while the rest of the body text remains preserved.
Frequently Asked Questions and Precautions
1. What is the difference between this function and ordinary find and replace?
Ordinary find and replace usually only substitutes a matched word or text segment. For example, replacing “Annex A” with “Annex A - Q&A” might leave the old description after the keyword in the line. However, "Find and replace complete lines in text files based on keywords" treats the entire line as the processing unit; as long as the line contains the keyword, the whole line is replaced with the new line, making it more suitable for updating directory lines, configuration lines, title lines, and description lines.
2. Why must the lists on the left and right sides correspond?
The left side is the search condition, and the right side is the replacement result. During batch processing, the software performs replacements according to the set relationship. To avoid misalignment of replacement results, it is recommended to check line by line after inputting. For example, if the first line on the left is “Annex A”, the first line on the right should be “Annex A - Q&A”.
3. Can many TXT files be processed at once?
Yes. As seen from the screenshots, the software supports adding multiple files and also supports importing files from a folder. For a large number of txt files, importing from a folder will be more efficient. After importing, you should confirm the processing scope through the file names, paths, and extension information in the list.
4. Is a backup needed before processing?
Batch replacement will affect multiple files. It is recommended to keep a backup of the original files before formal processing, especially when dealing with business materials, configuration texts, customer lists, or historical archive files. This way, even if keyword settings are wrong, recovery can be done quickly.
5. Should the keyword be the complete line or a partial keyword?
This function locates the entire line based on the keyword, so you usually only need to write a keyword that can uniquely identify the target line. In the example, writing “Annex A” is sufficient to locate the target line containing that text. However, if “Annex A” appears in multiple places within a file, all lines containing that keyword may be processed, so the keyword should be as precise as possible.
Summary: Reduce repetitive editing with batch processing, improve TXT file maintenance efficiency
When multiple text files contain the same or similar old line content, manually opening and modifying TXT files one by one is time-consuming and error-prone. Leveraging the "Find and replace complete lines in text files based on keywords" function in HeSoft Doc Batch Tool , you can first batch import the txt files, then set the list of keywords to find and the corresponding new line content, and finally complete the save settings and start processing through the wizard-style workflow.
This method is particularly suitable for office scenarios like batch replacing whole TXT lines, batch updating text file directories, batch modifying configuration instructions, and unifying text document content. It is recommended to test the effect with a small number of files first, confirm the keywords and replacement content are correct, and then execute the batch processing on the complete folder. This ensures accuracy and truly realizes the value of office software in batch processing files, reducing repetitive labor, and improving efficiency.
Keyword:Batch replace entire lines in TXT , text file keyword replacement , batch process text files , TXT batch find and replace