Skip to content

thees-k/replace-strings-cli

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 

Repository files navigation

replace_strings.py

A simple command-line tool to perform regex-based string replacements in text files.

I needed it for programming.

Mainly implemented by Claude Sonnet 4.6.

Features

  • Use Python regular expressions to search and replace text.
  • Supports a test mode (-t flag) to preview changes line-by-line without modifying the file.
  • Handles common file errors gracefully (file not found, permission denied).

Usage

python replace_strings.py [-t] "search pattern" "replacement string" textfile
  • -t (optional): Preview changes without modifying the file.
  • "search pattern": A Python regex pattern to search for.
  • "replacement string": The string to replace matches with.
  • textfile: Path to the text file to modify.

Examples

Many examples can be found in test_replace_strings.py.

Simple replacement without using regex

replace_strings.py "Hello" "Hi" mytext.txt

Note that "hello" would not be replaced. The script works case sensitive.

Preview changes without modifying the file

replace_strings.py -t "Hello" "Hi" mytext.txt

Replace one or more digits with "#"

replace_strings.py "\d+" "#" mytext.txt

Remove all lines that contain only whitespace

replace_strings.py "(?m)^\s+$" "" mytext.txt

Swap first two words of each line and add comma

e.g. "John Smith" → "Smith, John"

replace_strings.py "(\w+) (\w+)" "\2, \1" mytext.txt

Reformat ISO dates

e.g. "2026-03-06" → "06.03.2026".

replace_strings.py "(?P<y>\d{4})-(?P<m>\d{2})-(?P<d>\d{2})" "\g<d>.\g<m>.\g<y>" mytext.txt

Installation

No installation required. Requires Python 3.7+.

Testing

The project includes a helper function in test_replace_strings.py to mirror the core replacement logic for testing purposes.

Error Handling

  • Invalid regex patterns will print an error and exit.
  • File not found or permission errors will print an error and exit.

License

MIT License

About

Command-line tool to perform regex-based string replacements in text files written

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages