TSTool / Command / TextEdit
Overview
The TextEdit command edits a text file,
currently providing simple search and replace for literal strings.
The replacement text can be provided using the ReplaceWith command parameter or from a file
using the ReplaceWithFile command parameter.
The search and replace functionality is implemented using Java pattern matching and regular expressions
(see the Java Pattern class documentation).
It can be complicated to deal with special characters and patterns as they are processed
through TSTool commands and underlying Java code.
For example, some patterns pass through to the underlying code without changes whereas others
need to be transformed or "escaped" to protect from manipulation by TSTool.
The following examples illustrate how to perform common search and replace tasks.
See also other commands that manipulate properties such as
the ReadPropertiesFromFile command,
which can be used to read one or more properties from a text file
and the property values can be used to replace text in an initial file.
Command Editor
The command is available in the following TSTool menu:
- Commands / General - File Handling
The following dialog is used to edit the command and illustrates the command syntax.
TextEdit Command Editor (see full-size image)
Command Syntax
The command syntax is as follows:
TextEdit(Parameter="Value",...)
Command Parameters
| Parameter | Description | Default |
|---|---|---|
InputFilerequired |
The name of the input file. Can be specified using ${Property}. |
None - must be specified. |
SearchForrequired |
The string to search for in the input file.
|
None - must be specified. |
ReplaceWith |
The string to use for the replacement string in the output file:
|
Must be specified if ReplaceWithFile is not specified. |
ReplaceWithFile |
The name of a file containing text to use for the ReplaceWith text. The entire file is used for the replacement, including end of line characters. The file name and content can use ${Property} syntax. |
Must be specified if ReplaceWith is not specified. |
OutputFilerequired |
The name of the output file. Can be specified using ${Property}. |
None - must be specified. |
IfInputNotFound |
Indicate an action if the source file is not found: Ignore (ignore the missing file and do not warn), Warn (generate a warning message), Fail (generate a failure message) |
Warn |
Search and Replace Example Patterns
The following table lists various combinations of SearchFor and ReplaceWith command parameter values.
The dollar sign has special meaning to the pattern matcher (end of line) and braces also have meaning. Using ${ } directly causes TSTool to try to replace with a property value. Using backslashes in front of characters allows the string to pass through TSTool without being interpreted as a processor ${Property} so that the pattern matcher can handle.
Search and Replace Example Patterns
SearchFor |
ReplaceWith |
Description |
|---|---|---|
\r\n |
\n |
Replace Windows end of line with Linux end of line. |
\n |
\r\n |
Replace Linux end of line Windows end of line with Linux end of line. |
StationId |
\$\{StationId} |
Replace the static string StationId with a property ${StationId}. |
Some.*Text |
SomeNewText |
The * typically cannot be used alone and therefore .* indicates "any character followed by one or more characters". The example shows how intervening characters in the original string can be replaced with new text. |
SomeFile\..*\.ext |
SomeFile\.ABC\.ext |
Similar to previous example except escape the period from interpretation by pattern matcher and treat literally, needed when handling filenames with extensions. |
\$\{StationId\} |
1234 |
Replace the string ${StationId} with a literal string 1234. |
\$\{StationId\} |
${StationId} |
Replace the string ${StationId} with the property value in {StationId}. |
Examples
See the automated tests.
Troubleshooting
See the main TSTool Troubleshooting documentation.
See Also
AppendFilecommandCopyFilecommandReadPropertiesFromFilecommandRemoveFilecommand