You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Enhance exact text replacement functionality in line_in_file
- Updated documentation to clarify that the `exact` action treats special characters (dots, brackets, parentheses, etc.) as literal characters, ensuring true literal matching.
- Added new functions `escape_sed_pattern` and `escape_sed_replacement` to properly escape special characters for use in sed, improving the reliability of text replacements in configuration files.
| `ensure` | Ensures specified lines exist in the file. | One or more lines to ensure. | Appends the line(s) to the end of the file. |
130
130
| `replace` | Replaces a line containing specific content with a new line. | Two arguments: search content and replacement line. | Appends the replacement line to the file. |
131
131
| `after` | Inserts a new line after a line containing specific content. | Two arguments: search content and line to insert. | Appends both the search content and new line to the file. |
132
-
| `exact` | Performs an exact replacement of text within the file. | Two arguments: exact text to replace and replacement text. | Returns an error. |
132
+
| `exact` | Performs an exact replacement of text within the file. Special characters (dots, brackets, parentheses, etc.) are treated as literal characters. | Two arguments: exact text to replace and replacement text. | Returns an error. |
133
133
| `search` | Searches for specified content in the file. | One argument: content to search for. | Returns false (exit code 1). |
134
134
| `delete` | Deletes lines containing specified content. | One argument: content to delete. | Returns an error if the content is not found. |
135
135
@@ -169,6 +169,18 @@ label: Perform an exact text replacement
169
169
line_in_file --file document.txt --action exact "old text" "new text"
170
170
```
171
171
::
172
+
173
+
::code-panel
174
+
---
175
+
label: Replace text with special characters (dots, parentheses, etc.)
@@ -191,6 +203,7 @@ The function will output error messages to stderr and return a non-zero exit cod
191
203
192
204
- The function uses `sed_inplace` for in-place file modifications. Ensure this function is available in your environment.
193
205
- When using the `replace` action, forward slashes in the replacement string are automatically escaped.
206
+
- When using the `exact` action, all regex special characters (dots, brackets, parentheses, etc.) are automatically escaped, ensuring true literal matching. This makes it perfect for replacing configuration values, URLs, or any text containing special characters.
194
207
- The function creates the target file if it doesn't exist.
0 commit comments