Batch Replacement of Entire Lines in TXT Text Files: Use Regex Wildcards to Replace Lines Containing a Year with New Content
Translation:EnglishFrançaisDeutschEspañol日本語한êµì–´ï¼ŒUpdate Time:2026-08-04 06:50:39
In many TXT text files, if a line contains the same type of keyword, such as year, serial number, order number, version number, or fixed identifier, opening each file to edit it is not only time-consuming but also prone to omissions. This article explains how to use HeSoft Doc Batch Tool to search for entire lines containing keywords using wildcard regular expressions, and batch replace them with new line content. It is suitable for processing large volumes of TXT text documents, logs, lists, and instruction files.
In daily office work, a lot of data is not stored in Word, Excel, or PDF files but exists as plain text (.txt) files. Examples include project notes, configuration lists, directory texts, exported log files, and segments from batch-generated reports. These files seem simple, but once the number of files increases, making repetitive changes becomes very troublesome. For example, if multiple .txt files contain a line with the year 2020, and you now need to replace the entire line containing that year with new content. Manually opening 1.txt, 2.txt, 3.txt, 4.txt, and 5.txt one by one to find and edit is not only inefficient but also prone to missing items due to slight differences in line content.
The problem this article aims to solve is: how to use the batch processing capabilities of office software to match keywords with wildcard regular expressions and replace all lines containing the keyword in multiple text files with a new line at once. The example uses HeSoft Doc Batch Tool , positioned as batch document and file processing office software, suitable for reducing repetitive work, especially for batch processing .txt, text data, and other common office files.
Applicable Scenarios: Which situations are suitable for batch replacing entire lines containing keywords
This type of operation is suitable for processing text files with similar structures but where each file's content is not completely identical. Ordinary find and replace usually only replaces a certain word, while what this article demonstrates is locating a line based on a keyword and then replacing the entire line, making it more suitable for cleaning up or standardizing certain records.
Common scenarios include: batch replacing title lines containing a certain year with a new version title in .txt files; batch deleting or replacing configuration lines containing specific numbers; batch modifying entire lines of descriptions containing a certain error code in log files; batch standardizing entry names in multiple directory texts; batch replacing text lines containing version numbers, dates, order numbers, or customer IDs with standard content. Since the search criteria support fuzzy formula searching, meaning wildcard rules similar to regular expressions can be used, the processing is more flexible when keywords are not fixed text but patterned content like years, numbers, or IDs.
Effect Preview: What changes before and after processing
Before Processing: Multiple .txt files needing a unified modification
The example folder contains 5 .txt text files: 1.txt, 2.txt, 3.txt, 4.txt, and 5.txt. All these files need similar content processing. If done manually, you would have to open, search, replace, and save each one, with the time consumption becoming more apparent as the number of files increases.

Opening one of the files shows that the third line of text contains the year 2020, with the full line content similar to "Annex A - Food and drink standards - revised 2020". The goal here is not just to replace "2020", but to replace the entire line containing "2020" with a new, complete line.

After Processing: The entire line containing the keyword is replaced with new content
After the batch processing is complete, opening the same .txt file again shows that the full line originally containing the year 2020 has now become "Annex A - Q&A". Other lines that did not match the keyword remain unchanged, for instance, Annex B, Annex C, Contents, and the directory content below are still preserved in their original form.

This result illustrates that the software does not simply replace a few characters but locates the complete line based on a keyword and then overwrites the original line with a specified new one. For office tasks requiring the batch standardization of text entries, this method is more reliable than manual replacement.
Operational Steps: Using regular expressions for batch replacing text lines containing keywords
Step 1: Enter the text tool and select the entire line replacement function
Open HeSoft Doc Batch Tool , and select "Text Tools" in the tool category on the left. The main interface will display multiple batch functions related to text, such as finding and replacing keywords in text, deleting blanks in text, text to Word, text to PDF, etc. The function to be used in this article is for finding and replacing complete lines in text files based on a keyword.

The purpose of selecting this function is to tell the software that the processing target this time is not an individual word but the entire line of text containing the keyword. After entering the function, the software will guide you through the process of file selection, processing options, save location, and starting the process.
Step 2: Add the .txt files needing batch processing
After entering the function page, the first step is to select the records to be processed. Buttons like Add File, Import Files from Folder, Clear, and More can be seen at the top of the interface. If you are only processing a few files, you can click "Add File"; if the files are concentrated in the same directory, "Import Files from Folder" is more convenient.

In the example, 5 .txt files have been imported, and the list shows information such as number, name, path, extension, creation time, modification time, etc. You can check here whether all files have been added and confirm there are no mistaken or missed selections. If a certain file does not need processing, you can remove it from the list using the delete icon in the operation column. After confirming the list is correct, click "Next" at the bottom.
Step 3: Set the search method and the replacement line content
On the Processing Options settings page, first pay attention to the search method. The interface provides two options: Exact Text Search and Formula Fuzzy Text Search. Since this example needs to match patterned content like years, select Formula Fuzzy Text Search. The formula here can be understood as wildcard or regular expression rules, suitable for matching a type of text rather than just fixed characters.

In the keyword list to be found, enter \d{4}. This expression is commonly used to match four consecutive digits, which is consistent with "2020" in the example text. Then, in the replacement keyword list, enter the new full-line content, such as "Annex A - Q&A". Since the current function is to find and replace complete lines in text files, any line containing the matched four-digit number will be replaced by the new content set on the right side.
If your actual need is not to match a year but a fixed word, you can select "Exact Text Search" and enter the specific keyword; if you want to match numbers, dates, or other patterned characters, you can continue using Formula Fuzzy Text Search. After completing the settings, click "Next".
Step 4: Set the save location and start the process
Next, proceed to set the save location. To facilitate result verification, it is recommended not to overwrite the only original copy directly; instead, save the processed files to a new folder. This way, even if the rule settings do not meet expectations, you can go back to the original files and process them again. After confirming the save location, enter the start processing step to launch the batch task.
Once processing is complete, open the output file to check the results. In the example, the line containing 2020 has been replaced by "Annex A - Q&A", while other lines remain undisturbed, indicating the batch replacement rules took effect.
Frequently Asked Questions and Precautions
1. Why use Formula Fuzzy Text Search
When keywords are fixed, Exact Text Search is more straightforward; but if keywords are variable content like years, serial numbers, dates, or sequence numbers, Formula Fuzzy Text Search is more suitable. For instance, \d{4} can match four-digit numbers without needing to input specific values like 2020, 2021, 2022 individually.
2. Is the keyword or the entire line being replaced
The function name used in this article clearly points to complete line replacement. That is, matching the keyword only serves to locate which line needs processing, and what gets replaced ultimately is the entire line containing that keyword, not just the keyword itself.
3. Is a backup needed before batch processing
It is recommended to keep the original files or output to a new directory. The value of batch processing lies in handling a large number of files at once, but if the rules are set inaccurately, it can also affect the results in bulk. Testing with a small number of samples first, then processing all files, is a more stable office workflow.
4. Is this only suitable for .txt files
The screenshots demonstrate with .txt text files. HeSoft Doc Batch Tool also provides categories for Word tools, Excel tools, PowerPoint tools, PDF tools, etc., but the specific function described in this article is for processing complete lines in text files. To process files like doc, docx, xlsx, or PDF, you should choose the corresponding tool and function.
Summary: Using batch processing to reduce repetitive opening and manual editing
When many .txt text files all need lines containing a keyword found and uniformly replaced, manual processing is not only slow but prone to errors. With the help of HeSoft Doc Batch Tool , you can first import multiple text files, then use wildcard regular expressions to locate the target lines, and finally replace these lines with new standard content all at once. For office tasks like batch modifying year lines, serial number lines, configuration lines, and directory lines, this method can significantly reduce repetitive labor and improve file processing efficiency. It is recommended that before official processing, you first prepare a few sample files to test the rules and confirm the results are correct before processing all files in batch.
Keyword:Batch TXT Replacement of Entire Lines , Regex Search , Batch Text File Processing