Skip to content

A perl script that implements a simple find-and-replace based ona config "dictionary". Includes sample to translate British to American words (and vice versa)

License

Notifications You must be signed in to change notification settings

tigger04/sanitize-writing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Sanitize Writing Script

A Perl script that processes text files to replace specified patterns.

Description

This script applies word-based pattern replacements defined in a configuration file

Prerequisites

  • Perl 5
  • Required Perl modules:
    • File::Copy
    • File::Basename
    • Cwd
    • Getopt::Long

Usage

./sanitize-writing.pl --template=CONFIG_FILE [options] text_file

Options

--template=CONFIG_FILE
Path to the configuration file containing replacement patterns
-b, --backup
Create a backup of the original file before processing
--backup-file=FILENAME
Specify custom backup filename (optional, requires -b)
text_file
Path to the text file to be processed

Configuration File Format

The configuration file should contain replacement patterns in the following format:

search=replace

Each line defines a single replacement rule:

  • Empty lines are ignored
  • Lines starting with # are treated as comments
  • Patterns are case-insensitive
  • Words are matched with word boundaries

Example configuration:

# Replace common abbreviations
govt=government
dept=department

Features

  • Optional backup creation with -b flag
    • Default backup uses .bak extension
    • Custom backup filename supported
  • Word boundary matching to prevent partial word replacements
  • assumes UTF-8 encoding
  • Preserves original file on error

Error Handling

The script will exit with an error message if:

  • Required files are missing
  • Command-line arguments are incorrect
  • File operations fail

Output

  • Reports each successful pattern replacement with count
  • Creates backup files when requested
  • Displays completion message

Examples

Process a document using a custom template:

./sanitize-writing.pl --template=my_rules.conf document.txt

Create backup with default name (.bak extension):

./sanitize-writing.pl --template=my_rules.conf -b document.txt

Create backup with custom filename:

./sanitize-writing.pl --template=my_rules.conf -b --backup-file=document.backup.txt document.txt

Files

sanitize-writing.pl
Main script
CONFIG_FILE
User-provided replacement patterns
  • See words-uk-to-us.txt and words-us-to-uk.txt as examples (British to American and American to British English word replacements)

About

A perl script that implements a simple find-and-replace based ona config "dictionary". Includes sample to translate British to American words (and vice versa)

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages