Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

FindReplace in a file

Aug
2,293
111
Further to FindReplace in a file,
using the ReplaceText command from the TextUtils plugin is another fast method to find and replace text in a text file;
Code:
ReplaceText "e:\cobolscript\cobcal.cbl" /w /r:"CALENDAR USING 2024 12":"CALENDAR USING %_year %_month" > r:\cobcal.cbl

Code:
REPLACETEXT — Replace strings in text from a file.

Syntax:
REPLACETEXT /A:attribs /C /CP:n /H /N /P /R:from:to /S /W /X:from:to filename…

/A:attribs    attributes mask; valid flags are -ACEHIORS
/C    replace character escapes (affects following /R: and /X:)
/CP:n    interpret non-Unicode input text using code page n
/H    display filenames
/N    disable features
/P    page output
/R:from:to    specify old and replacement text
/S    search in subdirectories for matching files
/W    whole words only (affects following /R: and /X:)
/X:from:to    specify old and replacement text (do not auto-capitalize)
…    Range options are also supported.
If standard input (stdin) is redirected, REPLACETEXT will read from stdin before any filenames specified on the command line. If no filenames are specified, then REPLACETEXT will read from stdin whether it is redirected or not. Filenames may include wildcards and directory aliases. You can search into subdirectories for matching files with /S. @File lists and internet files are supported. You may also specify CLIP: to read from the clipboard.

If you want to pipe to REPLACETEXT, remember that pipes open a new shell. To pipe to a plugin command, you must either ensure that the plugin is loaded in the transient shell, e.g. by installing the .DLL in the shell’s .DLL directory; or else use temporary files or an in-process pipe.

Use /R: or /X: to specify the strings to search for (from) and to substitute (to). You must have at least one of these; you may add as many as you like. The text from each matching file will be dumped to stdout, with every occurrence of from replaced with the corresponding to string. If you give a from string without a matching to, then matching strings will simply be omitted from the output. The difference between the two options is that /R: automatically capitalizes the to string to match the from text which it replaces, but /X: does not. The rules for /R: are simple:

The first two letters in the matching text are examined.
If the first letter is lowercase, the to text is used unchanged.
If the first letter is uppercase but the second is lowercase, only the first letter in to is forced to uppercase.
If both the first two letters are uppercase, all of the to string is forced to uppercase.
/W only affects those /R: and /X: options which follow it on the command line. /W prevents matching text which immediately follows or immediately precedes a letter or digit.

/C only affects those /R: and /X: options which follow it on the command line. /C expands character escapes of the form \nnn (decimal) or \Xxx (hexadecimal) in both the from and to text. Use this option to embed troublesome characters. For example, you could use /C /R:\x22: to strip double-quote marks from a file.

/N disables features:

/NB    do not write a Byte Order Mark
/NC    disable highlight
/ND    do not search into hidden directories; only useful with /S
/NF    suppress the file-not-found error
/NJ    do not search into junctions; only useful with /S
/NZ    do not search into system directories; only useful with /S
You can combine these, e.g. /NDJ.


replacetext "Engine Summer.txt" /w /r:winter:autumn /r:but:yet

Joe
 
Back
Top