{"id":13501649,"url":"https://github.com/aureliojargas/replace","last_synced_at":"2025-12-27T05:37:55.601Z","repository":{"id":54059821,"uuid":"127319821","full_name":"aureliojargas/replace","owner":"aureliojargas","description":"Generic file search \u0026 replace tool (python)","archived":false,"fork":false,"pushed_at":"2024-03-26T00:11:26.000Z","size":58,"stargazers_count":32,"open_issues_count":1,"forks_count":5,"subscribers_count":6,"default_branch":"main","last_synced_at":"2024-10-31T20:40:43.681Z","etag":null,"topics":["batch-processing","find-and-replace","python3","regex"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/aureliojargas.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-03-29T16:42:11.000Z","updated_at":"2024-07-31T18:49:55.000Z","dependencies_parsed_at":"2024-03-31T00:01:01.616Z","dependency_job_id":null,"html_url":"https://github.com/aureliojargas/replace","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aureliojargas%2Freplace","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aureliojargas%2Freplace/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aureliojargas%2Freplace/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aureliojargas%2Freplace/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aureliojargas","download_url":"https://codeload.github.com/aureliojargas/replace/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246167091,"owners_count":20734379,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["batch-processing","find-and-replace","python3","regex"],"created_at":"2024-07-31T22:01:44.950Z","updated_at":"2025-12-27T05:37:55.565Z","avatar_url":"https://github.com/aureliojargas.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"# replace\n\nGeneric file search \u0026 replace tool, written in Python.\n\n## Options\n\nSpecify the FROM/TO patterns directly in the command line:\n\n```\n-f, --from TEXT         specify the search text or regex\n-t, --to TEXT           specify the replacement text\n```\n\nSpecify the FROM/TO patterns using files, useful for multiline matches:\n\n```\n-F, --from-file FILE    read the search text from this file\n-T, --to-file FILE      read the replacement text from this file\n```\n\nThe default search uses simple string matching, no magic.\nBut if you need power, there you have it:\n\n```\n-r, --regex             use regex matching instead of string matching\n```\n\nJust like `sed`, this script by default show results to STDOUT.\nBut instead, you can save the edits to the original file:\n\n```\n-i, --in-place          edit files in-place\n```\n\n\n## Examples\n\n```bash\n# Replace all mentions of old.css with new.css in all HTML files\nreplace --from old.css --to new.css --in-place *.html\n\n# Update the AdSense code in all HTML files\n# The old and the new code are in separate files\nreplace --from-file adsense.old --to-file adsense.new -i *.html\n\n# Enclose all numbers inside square brackets: 123 -\u003e [123]\nreplace --regex --from '(\\d+)' --to '[\\\\1]' file.txt\n\n# From http to https in all HTML files, recursive\nfind . -type f -name \"*.html\" \\\n    -exec replace \\\n      -f 'http://example.com' \\\n      -t 'https://example.com' \\\n      -i {} \\;\n```\n\n## Tests\n\nThe following command lines are executed and verified by [clitest](https://github.com/aureliojargas/clitest), using the `clitest README.md` command.\n\nFirst, setup a sample text file:\n\n```console\n$ echo 'the quick brown fox' \u003e file.txt\n$ cat file.txt\nthe quick brown fox\n$\n```\n\nNow 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.\n\n```console\n$ ./replace.py --from 'brown' --to 'red' file.txt\nthe quick red fox\n$ ./replace.py -f 'brown' -t 'red' file.txt\nthe quick red fox\n$ ./replace.py -f 'o' -t '◆' file.txt\nthe quick br◆wn f◆x\n$\n```\n\nFor 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.\n\n```console\n$ ./replace.py --regex -f '[aeiou]' -t '◆' file.txt\nth◆ q◆◆ck br◆wn f◆x\n$ ./replace.py -r -f '[aeiou]' -t '◆' file.txt\nth◆ q◆◆ck br◆wn f◆x\n$\n```\n\nIf necessary, you can also apply the replacements on text coming from STDIN, using `-` as the file name.\n\n```console\n$ cat file.txt | ./replace.py -r -f '[aeiou]' -t '◆' -\nth◆ q◆◆ck br◆wn f◆x\n$\n```\n\nNote 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:\n\n```console\n$ ./replace.py -r -f '[aeiou]' -t '◆' -i file.txt\nSaved file.txt\n$ cat file.txt\nth◆ q◆◆ck br◆wn f◆x\n$\n```\n\nSome boring tests for missing or incomplete command line options:\n\n```console\n$ ./replace.py 2\u003e\u00261 | grep error\nreplace.py: error: the following arguments are required: FILE\n$ ./replace.py README.md\nError: No search pattern (use --from or --from-file)\n$ ./replace.py -f '' README.md\nError: No search pattern (use --from or --from-file)\n$ ./replace.py -f foo README.md\nError: No replace pattern (use --to or --to-file)\n$\n```\n\nOK, we're done for now.\n\n```console\n$ rm file.txt\n$\n```\n\n## Similar tools\n\n- https://github.com/dmerejkowsky/replacer/\n- https://github.com/facebook/codemod\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faureliojargas%2Freplace","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faureliojargas%2Freplace","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faureliojargas%2Freplace/lists"}