How to Use Text Filters

Instructional HowTos, posted by users. No questions here please.

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
User avatar
bbadmin
Site Admin
Posts: 863
Joined: Mon Feb 17, 2003 8:54 pm
Contact:

How to Use Text Filters

Post by bbadmin »

Text filters can be written in scripting languages such as Python, PHP and awk. Selected text can be filtered through them to perform edits which TextPad cannot do natively. Any script or program which reads from stdin and writes to stdout can be employed. Anything written to stderr will appear in the Tool Output window.

The following screenshots show how to add a text filter written in PHP and then run it on a selected column of numbers to calculate their total.
  1. Add a filter:
    1-AddFilter.png
    1-AddFilter.png (33.14 KiB) Viewed 1763 times
  2. Select a scripting language:
    2-SelectPHP.png
    2-SelectPHP.png (46.05 KiB) Viewed 1763 times
  3. Give it a meaningful name:
    3-RenameFilter.png
    3-RenameFilter.png (24.86 KiB) Viewed 1763 times
  4. Apply it:
    4-ApplyFilter.png
    4-ApplyFilter.png (25.47 KiB) Viewed 1763 times
  5. Specify the script in the Parameters box then click OK:
    5-SpecifyScript.png
    5-SpecifyScript.png (20.7 KiB) Viewed 1763 times
  6. This the sample script in PHP: (it's in TextPad's Samples folder.)
    6-PHPscript.png
    6-PHPscript.png (17.91 KiB) Viewed 1763 times
  7. Select the column in block mode and run the script:
    7-RunFilter.png
    7-RunFilter.png (50.56 KiB) Viewed 1763 times
  8. The result:
    8-FilterResult.png
    8-FilterResult.png (18.16 KiB) Viewed 1763 times
PHP can be installed from https://windows.php.net/download/.
Last edited by bbadmin on Sat Jun 08, 2024 1:24 pm, edited 6 times in total.
User avatar
bbadmin
Site Admin
Posts: 863
Joined: Mon Feb 17, 2003 8:54 pm
Contact:

Re: How to Use Text Filters

Post by bbadmin »

Here is another example using HTML Tidy to validate and pretty print an HTML document.

HTMLtidy.png
HTMLtidy.png (21.12 KiB) Viewed 1757 times

Any errors are written to the Tool Output window.

HTML Tidy can be installed from http://binaries.html-tidy.org/
Last edited by bbadmin on Fri May 31, 2024 5:55 pm, edited 1 time in total.
User avatar
bbadmin
Site Admin
Posts: 863
Joined: Mon Feb 17, 2003 8:54 pm
Contact:

Re: How to Use Text Filters

Post by bbadmin »

Here is a Python script to pretty print a JSON document:

Code: Select all

# PrettifyJson.py
# Pretty prints JSON.
# Select all before running it as a filter.

from sys import stdin
import json

obj = json.load(stdin)
prettyobj = json.dumps(obj, indent=4)
print(prettyobj)
This script is installed in TextPad's Samples folder.
Python can be installed from the Windows App Store.
User avatar
bbadmin
Site Admin
Posts: 863
Joined: Mon Feb 17, 2003 8:54 pm
Contact:

Re: How to Use Text Filters

Post by bbadmin »

Awk is a powerful scripting language which has been around since the early days of Unix. It can do a surprising amount in a single line, as this example to delete consecutive duplicate lines in a document shows. The script in the Parameters field is "!a[$0]++".

Screenshot 2024-06-08 163556.png
Screenshot 2024-06-08 163556.png (20.56 KiB) Viewed 1740 times

Awk can be installed with Google's Chocolatey using "choco install awk".

(Note that duplicate lines can also be deleted by searching for the regular expression "^(.+)$\n\g1" and replacing it with "$1". However, this will require multiple passes if lines are duplicated more than once.)
Post Reply