{"id":20186021,"url":"https://github.com/neosmart/rewrite","last_synced_at":"2025-04-10T06:21:18.252Z","repository":{"id":54221962,"uuid":"82945397","full_name":"neosmart/rewrite","owner":"neosmart","description":"An in-place file rewrite utility, useful for redirecting output to same file as source.","archived":false,"fork":false,"pushed_at":"2021-03-03T20:05:51.000Z","size":23,"stargazers_count":26,"open_issues_count":1,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-24T07:26:56.826Z","etag":null,"topics":["command-line","command-line-tool","unix","utility"],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/neosmart.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-02-23T16:12:32.000Z","updated_at":"2024-04-19T05:34:30.000Z","dependencies_parsed_at":"2022-08-13T09:30:55.774Z","dependency_job_id":null,"html_url":"https://github.com/neosmart/rewrite","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/neosmart%2Frewrite","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/neosmart%2Frewrite/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/neosmart%2Frewrite/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/neosmart%2Frewrite/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/neosmart","download_url":"https://codeload.github.com/neosmart/rewrite/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247798858,"owners_count":20998068,"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":["command-line","command-line-tool","unix","utility"],"created_at":"2024-11-14T03:15:40.574Z","updated_at":"2025-04-10T06:21:18.231Z","avatar_url":"https://github.com/neosmart.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# A `rewrite` primer\n\n### What it is\n\n`rewrite` is a simple command-line utility that allows for the in-place rewrite of a file's\ncontents, even where the file is being read from as the input. This makes transforming the contents\nof a file via other standard unix utilities dead simple, even when they expect the input and output\nfiles/streams to be physically separate.\n\n### The problem\n\nYou have a sequence of chained operations/commands that reads from a given file `file`, and you want\nto replace the contents of `file` with the result of that chain of commands. If you try to redirect\nthe output of your script via something like `\u003e file` or even `| tee file`, you'll find that more\noften than not, you'll lose everything and corrupt your data. That's because the upstream command is\nreading from the same file that is being written to, overwriting the input with the output.\n\n### The solution\n\n`rewrite` makes it stupid easy to work around this problem. Just pipe the output of your\npipeline/workflow to `rewrite file` and you'll get the result you expected. Easy peasey!\n\n### An example\n\nSay we want to sort a file. We don't want to sort _a copy_ of the file, we want to sort _the file\nitself_ (obviously). Unfortunately, it's not that easy. Here's an example wherein we select 1024\nrandom words from a dictionary file and then want to sort the output.\n\n```sh\nshuf -n 1024 /usr/share/dict/words \u003e words.txt\n```\n\nWe can easily sort this list with the `sort` utility, but what happens when we try to save the\noutput to itself?\n\n```sh\nsort words.txt \u003e words.txt # don't do this!\n```\n\n**This will result in a complete loss of data**, as the shell will set up the output file handle\nbefore `sort` gets a chance to open the same file to read it. In the end, you get neither this nor\nthat and lose all data in the process!\n\nHere's what you would normally do instead:\n\n```sh\nsort words.txt \u003e temp\nmv temp words.txt\n```\n\nWhich is easy \u0026 straightforward enough, except when `sort` is part of a bigger workflow or a script,\nor when you forget, or when `temp` already exists, or when you don't have as straightforward of a\ncase and don't realize that your source and destination files are one and the same. `rewrite` to the\nrescue!\n\nHere's how simple using `rewrite` here would be:\n\n```sh\nsort words.txt | rewrite words.txt\n```\n\nInternally, `rewrite` does all the \"magic\" of reading from `stdin` and buffering the content until\nthe upstream command has finished executing, then writing the output to the named file accordingly.\n\n# Installing `rewrite`\n\n`rewrite` is written in rust for performance, safety, and out-of-the-box cross-platform support.\nInstalling `rewrite` (presuming it's not already available as a binary for your platform in your\nfavorite package manager) is as simple as\n\n```\ncargo install rewrite\n```\n\nPre-built binaries for Windows, Linux, FreeBSD, and OS X are available separately. Assistance\npackaging and distributing on platform-native package managers is welcome.\n\n# Dependencies\n\nAs of version 1.0, `rewrite` is free of any dependencies (native or otherwise).\n\n# License\n\n`rewrite` is released to the general public without warranty in hopes of being useful under the\nterms of the MIT license. `rewrite` was written by Mahmoud Al-Qudsi, and development is sponsored by\n[NeoSmart Technologies](https://neosmart.net/).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fneosmart%2Frewrite","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fneosmart%2Frewrite","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fneosmart%2Frewrite/lists"}