All Projects → aureliojargas → replace

aureliojargas / replace

Licence: other
Generic file search & replace tool, written in Python 3

Programming Languages

python
139335 projects - #7 most used programming language
Makefile
30231 projects

Projects that are alternatives of or similar to replace

RideShare-Trip-Stats
Chrome Extension to visualize your uber trip statistics
Stars: ✭ 61 (+117.86%)
Mutual labels:  regex
expand-brackets
Expand POSIX bracket expressions (character classes) in glob patterns.
Stars: ✭ 26 (-7.14%)
Mutual labels:  regex
Socially
Socially is a textView which is able to create separate clickable views according to your requirements.
Stars: ✭ 28 (+0%)
Mutual labels:  regex
is-regex
Is this value a JS regex?
Stars: ✭ 22 (-21.43%)
Mutual labels:  regex
IFIscripts
Detailed documentation is available here: http://ifiscripts.readthedocs.io/en/latest/index.html
Stars: ✭ 46 (+64.29%)
Mutual labels:  batch-processing
ocaml-re-nfa
OCaml code to construct an NFA from a regular expression
Stars: ✭ 44 (+57.14%)
Mutual labels:  regex
sortboard
A small ES6 library for easy sorting and filtering of elements.
Stars: ✭ 29 (+3.57%)
Mutual labels:  regex
antk
Redkato, - Indonesian anime scraper
Stars: ✭ 14 (-50%)
Mutual labels:  regex
django-redirects
↪️ ✅ redirects as they should be, with full control.
Stars: ✭ 32 (+14.29%)
Mutual labels:  regex
clausejs
Write contract once. Get data & function validators & conformers, an accurate & readable project contract, auto-generated API documentation, generative test coverage, plus more. A tool that enables a more predictable workflow for developing your JavaScript projects.
Stars: ✭ 29 (+3.57%)
Mutual labels:  regex
cryptaddress.now
A minimal service to detect which cryptocurrency an address corresponds to.
Stars: ✭ 23 (-17.86%)
Mutual labels:  regex
stat133-spring-2019
Course materials for Stat 133, Spring 2019, at UC Berkeley
Stars: ✭ 26 (-7.14%)
Mutual labels:  regex
logwatch
日志采集工具
Stars: ✭ 22 (-21.43%)
Mutual labels:  regex
pysorter
A command line utility for organizing files and directories according to regex patterns.
Stars: ✭ 40 (+42.86%)
Mutual labels:  regex
git-search-replace
A utility on top of Git for project-wide search-and-replace that includes filenames too
Stars: ✭ 42 (+50%)
Mutual labels:  regex
regextester
An elementary OS app
Stars: ✭ 38 (+35.71%)
Mutual labels:  regex
loadkit
Java 资源加载器,充分拓展ClassLoader#getResources(name)的能力,实现递归加载,支持普通风格 / 包名风格 / ANT风格 / 正则风格路径的资源加载同时支持自定义过滤器,通常作为框架的基础类库。
Stars: ✭ 39 (+39.29%)
Mutual labels:  regex
unmatcher
Regular expressions reverser for Python
Stars: ✭ 26 (-7.14%)
Mutual labels:  regex
js-diacritic-regex
Creates the inverse of transliterated string to a regex. What? Basically, diacritic insensitiveness
Stars: ✭ 20 (-28.57%)
Mutual labels:  regex
java-core
Collections of solutions for micro-tasks created while building modules as part of project. Also has very fun stuffs :)
Stars: ✭ 35 (+25%)
Mutual labels:  regex

replace

Generic file search & replace tool, written in Python 3.

Options

Specify the FROM/TO patterns directly in the command line:

-f, --from TEXT         specify the search text or regex
-t, --to TEXT           specify the replacement text

Specify the FROM/TO patterns using files, useful for multiline matches:

-F, --from-file FILE    read the search text from this file
-T, --to-file FILE      read the replacement text from this file

The default search uses simple string matching, no magic. But if you need power, there you have it:

-r, --regex             use regex matching instead of string matching

Just like sed, this script by default show results to STDOUT. But instead, you can save the edits to the original file:

-i, --in-place          edit files in-place

Examples

# Replace all mentions of old.css with new.css in all HTML files
replace --from old.css --to new.css --in-place *.html

# Update the AdSense code in all HTML files
# The old and the new code are in separate files
replace --from-file adsense.old --to-file adsense.new -i *.html

# Enclose all numbers inside square brackets: 123 -> [123]
replace --regex --from '(\d+)' --to '[\\1]' file.txt

# From http to https in all HTML files, recursive
find . -type f -name "*.html" \
    -exec replace \
      -f 'http://example.com' \
      -t 'https://example.com' \
      -i {} \;

Tests

The following command lines are executed and verified by clitest, using the clitest README.md command.

First, setup a sample text file:

$ echo 'the quick brown fox' > file.txt
$ cat file.txt
the quick brown fox
$

Now we'll do some replaces using string matching, which is the default. Note that there are short and long options (-f/--from) and that the replacement is performed globally: all occurrences are replaced.

$ ./replace.py --from 'brown' --to 'red' file.txt
the quick red fox
$ ./replace.py -f 'brown' -t 'red' file.txt
the quick red fox
$ ./replace.py -f 'o' -t '' file.txt
the quick br◆wn f◆x
$

For more powerfull searches, use -r or --regex to perform a regular expression match. You have access to the full power of Python's regex flavor.

$ ./replace.py --regex -f '[aeiou]' -t '' file.txt
th◆ q◆◆ck br◆wn f◆x
$ ./replace.py -r -f '[aeiou]' -t '' file.txt
th◆ q◆◆ck br◆wn f◆x
$

If necessary, you can also apply the replacements on text coming from STDIN, using - as the file name.

$ cat file.txt | ./replace.py -r -f '[aeiou]' -t '' -
th◆ q◆◆ck br◆wn f◆x
$

Note that all the previous replaces were not saved to the original file. This is the default behavior (just like sed). If you want to edit the original file, use the -i or --in-place options:

$ ./replace.py -r -f '[aeiou]' -t '' -i file.txt
Saved file.txt
$ cat file.txt
th◆ q◆◆ck br◆wn f◆x
$

Some boring tests for missing or incomplete command line options:

$ ./replace.py 2>&1 | grep error
replace.py: error: the following arguments are required: FILE
$ ./replace.py README.md
Error: No search pattern (use --from or --from-file)
$ ./replace.py -f '' README.md
Error: No search pattern (use --from or --from-file)
$ ./replace.py -f foo README.md
Error: No replace pattern (use --to or --to-file)
$

OK, we're done for now.

$ rm file.txt
$

Similar tools

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].