How to use regular expression wildcards to batch replace entire lines containing keywords in multiple TXT text files


Translation:EnglishFrançaisDeutschEspañol日本語한국어,Update Time:2026-08-03 06:50:43
Content note: This article reflects the software version available when it was published. Interfaces and features may change with updates; please refer to the current software. If you find an error, please let us know.
Software featured in this articleHeSoft Doc Batch Tool
Free DownloadSoftware IntroductionUser Guide

When multiple TXT text files contain old content with the same rules, such as entire line descriptions that include years, numbers, or a specific keyword, replacing them uniformly with new line content requires a time-consuming process of opening each file one by one to search and replace. This article uses HeSoft Doc Batch Tool as an example to demonstrate how to use the "Find and Replace Complete Lines in Text Files Based on Keywords" feature, utilizing wildcard rules similar to regular expressions to batch-locate entire lines containing keywords and replace them with the specified new text lines. This is suitable for batch maintenance of logs, directories, configuration texts, and material lists.

In daily office work and document management, many teams encounter this problem: a folder contains a large number of TXT text files, each with a similar content structure, but certain lines within them need to be updated uniformly. For example, a line might contain the year "2020", a version number, an order number, a project code, or a specific keyword. You don't just want to replace the keyword itself, but rather to directly replace the "entire line containing that keyword" with a new line of standard text.

If there are only one or two files, manually opening, finding, and modifying them is acceptable. But when the number of files reaches dozens or hundreds, repetitive operations not only waste time but also make it easy to miss changes or make mistakes. Especially for plain text materials like text files, log files, configuration files, and documentation, the content seems simple, but batch maintenance greatly tests tool efficiency.

This article introduces a more efficient processing method: using the office software " HeSoft Doc Batch Tool ", through the text tool's "Find and replace complete lines in text files based on keywords" function, combined with wildcard/regex-like search rules, to batch replace entire lines containing keywords in multiple TXT files with new lines. In the example, we will replace lines containing a four-digit year number with "Annex A - Q&A", thus achieving a unified update for multiple text files.

Applicable Scenarios: Which text batch replacement needs are suitable for this method

This method is suitable for processing "line-organized" text files, especially for batch replacing entire line content, rather than just replacing a few characters. Common scenarios include:

1. Batch updating description lines containing years or version numbers

For example, multiple TXT files all have lines like "revised 2020", "version 2023", "build 1001", and now these lines need to be uniformly changed to a new description. If you only use a normal find and replace, you might only replace the year itself, leaving the old remaining text intact. However, by replacing the "complete line", you can directly change the entire line to a standardized new line.

2. Batch replacing lines containing order numbers, codes, or identifiers

Many data lists, export records, and system logs contain identifiers, such as four-digit numbers, six-digit numbers, or codes with prefixes. Using wildcard or regex rules, you can locate lines matching a pattern and then replace the entire line with unified content.

3. Batch cleaning or rewriting certain lines in configuration text

If old parameter lines, old path lines, or old switch lines exist in multiple configuration files, you can use keywords to locate the entire line and replace it with a new configuration. It should be noted that the example in this article demonstrates TXT text file processing. For formats like Word's doc, docx, Excel's xls, xlsx, or PDF, you should select the corresponding document tool functions and not treat them directly as plain text.

4. Batch maintaining directory, appendix, and list-type texts

The example files contain directory-type content like Annex and Contents. For this kind of structured text, if a certain appendix entry needs to be uniformly renamed, batch replacement by complete line is more stable than modifying file by file.

Effect Preview: What changes after processing

Before the formal operation, let's first look at the goals and results of this batch processing. The example folder has 5 TXT files, named 1.txt, 2.txt, 3.txt, 4.txt, and 5.txt. Our goal is not just to process one file, but to execute the same rules on multiple text files at once.

image-Bulk replace entire lines of text,TXT regular expression replacement,wildcard batch processing of text files

Before Processing: Text lines contain keywords that need to be identified

Opening one of the TXT files reveals that the third line reads "Annex A - Food and drink standards - revised 2020". This line contains the four-digit number "2020". In this example, we consider such four-digit numbers as the keyword rule to match, using \d{4} to find text lines containing a four-digit number.

image-Bulk replace entire lines of text,TXT regular expression replacement,wildcard batch processing of text files

It is especially important to understand the meaning of "replacing the entire line": the software doesn't just replace "2020", but replaces the whole line containing "2020" with the new text. For instance, the original line "Annex A - Food and drink standards - revised 2020" will become "Annex A - Q&A" after processing.

After Processing: The entire line containing the keyword is replaced with the new line

After the batch processing is complete, open the file to check. You can see the third line has been changed to "Annex A - Q&A". Other lines that did not match the rule, such as Annex B, Annex C, and the directory entries under Contents, remain unchanged.

image-Bulk replace entire lines of text,TXT regular expression replacement,wildcard batch processing of text files

This is the core value of "Find and replace complete lines in text files based on keywords": as long as a line meets the search criteria, rewrite that entire line to the specified new content, thereby avoiding preserving old descriptions, version numbers, or notes.

Operation Steps: Using HeSoft Doc Batch Tool to batch replace complete lines

Below is a complete walkthrough of the operation process following the screenshot order. To ensure controllable results, it is recommended to prepare a test file or backup folder first, and process the official files only after confirming the rules are correct.

Step 1: Enter the text tool and select "Find and replace complete lines in text files based on keywords"

After opening " HeSoft Doc Batch Tool ", select "Text Tools" in the left function category. Find "Find and replace complete lines in text files based on keywords" in the function list. From the interface description, you can see that this function is used to batch delete lines containing a certain keyword in a text file or replace them with new text.

image-Bulk replace entire lines of text,TXT regular expression replacement,wildcard batch processing of text files

The purpose of this step is to enter the batch tool specifically for processing TXT text line content. Unlike the ordinary "Find and replace keywords in text", this function emphasizes "complete lines", making it suitable for needs like this example, where an entire line of old description needs to be replaced with a new one.

Step 2: Add the TXT files to be processed

After entering the function page, the interface will display the processing flow: select records to process, set processing options, set save location, start processing. In the first step, "Select records to process", you can select text files individually via "Add File", or use "Import files from folder" to import multiple TXT files from a folder all at once.

image-Bulk replace entire lines of text,TXT regular expression replacement,wildcard batch processing of text files

In the screenshot, 5 TXT files have been imported, located under D:\test\, all with the extension txt. The table shows information like file name, path, extension, creation time, and modification time. After confirming the file list is correct, click "Next" at the bottom.

The expected result of this step is: all text files to be batch processed appear in the list, and the record count matches the actual number of files to be processed. If there are files that don't need processing, you can remove them via the delete icon in the operation column to avoid accidental processing.

Step 3: Choose to use formula for fuzzy text search

After entering "Set processing options", you need to configure the search method. The screenshot shows "Use formula for fuzzy text search" is selected, not "Precise text search". This choice is crucial because this example needs to use a rule like \d{4} to match four-digit numbers, rather than searching for a fixed string.

image-Bulk replace entire lines of text,TXT regular expression replacement,wildcard batch processing of text files

If "Precise text search" is selected, the software will treat the input content as plain text. By selecting "Use formula for fuzzy text search", you can use more flexible expressions to locate text content. For example, \d{4} is typically used to represent four consecutive digits, capable of matching "2020", "2026", "1234" and other four-digit numbers.

Step 4: Enter the keyword rule to find

Enter in the "List of keywords to find":

\d{4}

Its meaning is to find text lines containing four consecutive digits. In the example file, "Annex A - Food and drink standards - revised 2020" contains "2020", so this entire line will be matched.

It's important to note here that if your actual need is not to match years but other keywords, you can adjust the search criteria accordingly. For example, to match lines containing "revised", enter the corresponding keyword. To match a certain type of code, you need to write a rule that can cover the identifier format. The more accurate the rule, the more reliable the batch replacement results.

Step 5: Enter the new line content for replacement

In the "List of keywords for replacement" on the right, enter the complete line content for replacement:

Annex A - Q&A

This means: any complete line matched by the left-side rule will be replaced with this new line of text. The red prompt in the screenshot indicates that if the replacement content is left blank, it might mean deletion. Therefore, if your goal is to replace with a new line, be sure to fill in the clear new content on the right side.

The final replacement logic in this example can be understood as: find the entire line containing four consecutive digits, and replace that entire line with "Annex A - Q&A".

Step 6: Set the save location and start processing

After completing the find and replace rules, click "Next" to enter "Set save location". The interface flow clearly shows this step. It is recommended to choose an appropriate output location based on actual needs. To facilitate backtracking, when batch processing official files, it's advisable not to directly overwrite the sole original copy, but to first output to a new folder to check the results.

After setting the save location, proceed to the "Start processing" step. Once processing is complete, open the output file to check the content, confirming that the matched lines have been replaced and unmatched lines remain unchanged.

Notes on Using Wildcards and Regex Rules

1. Confirm that you want to replace the "entire line" and not the "keyword itself"

The function used in this article processes the complete line containing the keyword. That is, once the rule is matched, the entire line will become the new content filled in on the right. If you only want to replace "2020" with "2026" while keeping the surrounding text, you should choose another function for replacing keywords, not this one.

2. Do not write rules too broadly

\d{4} will match any four consecutive digits. If the file also contains page numbers, other identifiers, amounts, or other four-digit numbers, they might also be matched. Before batch processing, it's recommended to first sample-check the file content to confirm that four-digit numbers only appear in the target lines to be replaced, or write a more specific rule.

3. Back up before multi-file batch processing

The advantage of batch processing is completing a large number of file modifications at once, which also means rule errors can be quickly amplified. Especially for content like TXT, logs, and configuration files, it is recommended to copy the original files first, or set the output location to a new folder, and only replace the official version after confirming correctness.

4. Note the case sensitivity option

The screenshot shows the option "Ignore letter case". If you don't want to distinguish case when searching for English keywords, you can check it. If case has business meaning, do not check it. This example uses a numeric rule and does not involve case sensitivity issues.

5. Leaving the replacement content empty may mean deletion

The right-side area prompts "Leaving blank means deletion". If your goal is to delete lines containing the keyword, you can leave the replacement content empty. If the goal is to replace with new text, you must fill in the complete new line to avoid accidental deletion.

Frequently Asked Questions

Why did the replacement fail after entering \d{4}?

First, confirm whether "Use formula for fuzzy text search" was selected for the search method. If "Precise text search" was still selected, the software may treat \d{4} as a literal string to find, rather than matching a four-digit number by rule. Second, check whether four consecutive digits actually exist in the file.

Can I process all TXT files in a folder at once?

Yes. From the operation screenshots, the function page provides "Import files from folder", suitable for importing multiple text files from the same directory at once. After importing, it's still recommended to check the list to ensure no files that shouldn't be processed are included.

Why did the entire line change after processing?

This is the design goal of this function. It's not a local keyword replacement, but it finds the complete line containing the keyword and then replaces the entire line with the new content on the right. Therefore, before using it, confirm that the requirement is indeed "replace the entire line".

Can it process doc, docx, or pdf files?

This article demonstrates batch processing of TXT text files. doc, docx belong to Word documents, and PDF is also not a plain text file in the ordinary sense; the processing methods differ from TXT. HeSoft Doc Batch Tool has different categories of office document processing entries. In actual use, you should choose the corresponding tool based on the file format.

Summary: Use batch processing to reduce repetitive opening and manual modification

When multiple TXT files have old line content following the same rule, manually modifying them one by one is both slow and error-prone. Through the "Find and replace complete lines in text files based on keywords" function of HeSoft Doc Batch Tool , you can centralize the repetitive actions of searching, locating, and replacing into a single batch task.

In this example, we used \d{4} to match text lines containing a four-digit number and replaced the matched complete lines with "Annex A - Q&A". After processing, the target lines were uniformly updated while other content remained unchanged. For office scenarios that require maintaining large numbers of TXT files, logs, lists, and configuration texts, this method of batch replacing entire lines can significantly reduce repetitive labor and improve file processing efficiency.

If you also need to batch replace complete lines containing keywords across multiple text files, it is recommended to first prepare a test file, follow the steps in this article to import files, set the fuzzy formula search rule, fill in the new line for replacement, and then output and check the results. Once the rule is confirmed accurate, you can confidently apply it to more files.


Keyword:Bulk replace entire lines of text , TXT regular expression replacement , wildcard batch processing of text files
Creation Time:2026-08-03 06:50:27
Software featured in this articleHeSoft Doc Batch Tool
Free DownloadSoftware IntroductionUser Guide
Content note: This article reflects the software version available when it was published. Interfaces and features may change with updates; please refer to the current software. If you find an error, please let us know.