{"id":18084941,"url":"https://github.com/jwodder/lineinfile","last_synced_at":"2025-04-12T20:10:38.084Z","repository":{"id":62576180,"uuid":"321157454","full_name":"jwodder/lineinfile","owner":"jwodder","description":"Add \u0026 remove lines in files by regex","archived":false,"fork":false,"pushed_at":"2025-01-23T14:40:21.000Z","size":132,"stargazers_count":10,"open_issues_count":6,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-04-08T12:16:32.542Z","etag":null,"topics":["add-line","available-on-pypi","edit","ensure-line","insert-line","python","regexp","remove-line","sed"],"latest_commit_sha":null,"homepage":"","language":"Python","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/jwodder.png","metadata":{"files":{"readme":"README.rst","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","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":"2020-12-13T20:50:16.000Z","updated_at":"2025-01-23T14:40:23.000Z","dependencies_parsed_at":"2023-01-31T02:01:00.002Z","dependency_job_id":"c5799aca-e0a6-4421-af8e-36e465feffa0","html_url":"https://github.com/jwodder/lineinfile","commit_stats":{"total_commits":99,"total_committers":1,"mean_commits":99.0,"dds":0.0,"last_synced_commit":"027b89fba8a353b843c928a10c587129c15eecea"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwodder%2Flineinfile","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwodder%2Flineinfile/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwodder%2Flineinfile/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jwodder%2Flineinfile/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jwodder","download_url":"https://codeload.github.com/jwodder/lineinfile/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248625493,"owners_count":21135513,"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":["add-line","available-on-pypi","edit","ensure-line","insert-line","python","regexp","remove-line","sed"],"created_at":"2024-10-31T15:08:44.059Z","updated_at":"2025-04-12T20:10:38.044Z","avatar_url":"https://github.com/jwodder.png","language":"Python","readme":"|repostatus| |ci-status| |coverage| |pyversions| |license|\n\n.. |repostatus| image:: https://www.repostatus.org/badges/latest/active.svg\n    :target: https://www.repostatus.org/#active\n    :alt: Project Status: Active — The project has reached a stable, usable\n          state and is being actively developed.\n\n.. |ci-status| image:: https://github.com/jwodder/lineinfile/actions/workflows/test.yml/badge.svg\n    :target: https://github.com/jwodder/lineinfile/actions/workflows/test.yml\n    :alt: CI Status\n\n.. |coverage| image:: https://codecov.io/gh/jwodder/lineinfile/branch/master/graph/badge.svg\n    :target: https://codecov.io/gh/jwodder/lineinfile\n\n.. |pyversions| image:: https://img.shields.io/pypi/pyversions/lineinfile.svg\n    :target: https://pypi.org/project/lineinfile/\n\n.. |license| image:: https://img.shields.io/github/license/jwodder/lineinfile.svg\n    :target: https://opensource.org/licenses/MIT\n    :alt: MIT License\n\n`GitHub \u003chttps://github.com/jwodder/lineinfile\u003e`_\n| `PyPI \u003chttps://pypi.org/project/lineinfile/\u003e`_\n| `Issues \u003chttps://github.com/jwodder/lineinfile/issues\u003e`_\n| `Changelog \u003chttps://github.com/jwodder/lineinfile/blob/master/CHANGELOG.md\u003e`_\n\nInspired by (but not affiliated with) `the Ansible module of the same name`__,\n``lineinfile`` provides a command and library for adding a line to a file if\nit's not already there and for removing lines matching a pattern from a file.\nThere are options for using a regex to find a line to update or to determine\nwhich line to insert before or after.  There are options for backing up the\nmodified file with a custom file extension and for treating a nonexistent file\nas though it's just empty.  There's even an option for determining the line to\ninsert based on capturing groups in the matching regex.\n\n__ https://docs.ansible.com/ansible/latest/collections/ansible/builtin/\n   lineinfile_module.html\n\nUnlike the Ansible module, this package does not perform any management of file\nattributes; those must be set externally.\n\n\nInstallation\n============\n``lineinfile`` requires Python 3.8 or higher.  Just use `pip\n\u003chttps://pip.pypa.io\u003e`_ for Python 3 (You have pip, right?) to install\n``lineinfile`` and its dependencies::\n\n    python3 -m pip install lineinfile\n\n\nExamples\n========\n\nA crude ``.ini``-file updater: Set ``theoption`` to ``value``, and if no\nsetting for ``theoption`` is found in the file, add one after the line\n\"``[thesection]``\":\n\n.. code:: console\n\n    $ lineinfile add \\\n        --after-first \"^\\[thesection\\]$\" \\\n        -e \"^theoption\\s*=\" \\\n        \"theoption = thevalue\" \\\n        settings.ini\n\nThe equivalent operation in Python:\n\n.. code:: python\n\n    from lineinfile import AfterFirst, add_line_to_file\n\n    add_line_to_file(\n        \"settings.ini\",\n        \"theoption = thevalue\",\n        regexp=r\"^theoption\\s*=\",\n        inserter=AfterFirst(r\"^\\[thesection\\]$\"),\n    )\n\nReplace the first instance of \"``foo = ...``\" with \"``foo = 'bar'``\",\npreserving indentation, and create a backup of the file with the extension\n``.bak``, even if no changes were made:\n\n.. code:: console\n\n    $ lineinfile add \\\n        -e \"^(\\s*)foo\\s*=\" \\\n        --backrefs \\\n        --match-first \\\n        --backup-always -i.bak \\\n        \"\\1foo = 'bar'\" \\\n        file.py\n\nThe equivalent operation in Python:\n\n.. code:: python\n\n    from lineinfile import ALWAYS, add_line_to_file\n\n    add_line_to_file(\n        \"file.py\",\n        r\"\\1foo = 'bar'\",\n        regexp=r\"^(\\s*)foo\\s*=\",\n        backrefs=True,\n        match_first=True,\n        backup=ALWAYS,\n        backup_ext=\".bak\",\n    )\n\n\nCommand-Line Usage\n==================\n\nThe ``lineinfile`` command has two subcommands, ``add`` and ``remove``.\n\n``add``\n-------\n\n::\n\n    lineinfile add [\u003coptions\u003e] \u003cline\u003e [\u003cfile\u003e]\n    lineinfile add [\u003coptions\u003e] -L \u003cline\u003e [\u003cfile\u003e]\n\nAdd the given ``line`` (after expanding backslash escapes) to the file if it is\nnot already present.  If a `Python regular expression`_ is given with the\n``-e``/``--regexp`` option and it matches any lines in the file, ``line`` will\nreplace the last matching line (or the first matching line, if\n``--match-first`` is given).  If the regular expression does not match any\nlines (or no regular expression is specified) and ``line`` is not found in the\nfile, the line is inserted at the end of the file by default; this can be\nchanged with the ``--after-first``, ``--after-last``, ``--before-first``,\n``--before-last``, and ``--bof`` options.\n\nIf no file name is given on the command line, input is read from standard\ninput, and the result is written to standard output.  It is an error to specify\nany of the ``--backup-changed``, ``--backup-always``, ``--backup-ext``, or\n``--create`` options when no file is given.\n\n.. _Python regular expression: https://docs.python.org/3/library/re.html\n                               #regular-expression-syntax\n\nOptions\n```````\n\n-a REGEX, --after-first REGEX\n                            If neither ``line`` nor ``--regexp`` is found in\n                            the file, insert ``line`` after the first line that\n                            matches the regular expression ``REGEX``, or at the\n                            end of the file if no line matches ``REGEX``.\n\n-A REGEX, --after-last REGEX\n                            If neither ``line`` nor ``--regexp`` is found in\n                            the file, insert ``line`` after the last line that\n                            matches the regular expression ``REGEX``, or at the\n                            end of the file if no line matches ``REGEX``.\n\n-b REGEX, --before-first REGEX\n                            If neither ``line`` nor ``--regexp`` is found in\n                            the file, insert ``line`` before the first line\n                            that matches the regular expression ``REGEX``, or\n                            at the end of the file if no line matches\n                            ``REGEX``.\n\n-B REGEX, --before-last REGEX\n                            If neither ``line`` nor ``--regexp`` is found in\n                            the file, insert ``line`` before the last line that\n                            matches the regular expression ``REGEX``, or at the\n                            end of the file if no line matches ``REGEX``.\n\n--bof                       If neither ``line`` nor ``--regexp`` is found in\n                            the file, insert ``line`` at the beginning of the\n                            file.\n\n--eof                       If neither ``line`` nor ``--regexp`` is found in\n                            the file, insert ``line`` at the end of the file.\n                            This is the default.\n\n-e REGEX, --regexp REGEX    If the given regular expression matches any lines\n                            in the file, replace the last matching line (or\n                            first, if ``--match-first`` is given) with\n                            ``line``.\n\n--backrefs                  If ``--regexp`` matches, the capturing groups in\n                            the regular expression are used to expand any\n                            ``\\n``, ``\\g\u003cn\u003e``, or ``\\g\u003cname\u003e`` backreferences\n                            in ``line``, and the resulting string replaces the\n                            matched line in the input.\n\n                            If ``--regexp`` does not match, the input is left\n                            unchanged.\n\n                            It is an error to specify this option without\n                            ``--regexp``.\n\n--backup, --backup-changed  If the input file is modified, create a backup of\n                            the original file.  The backup will have the\n                            extension specified with ``--backup-ext`` (or ``~``\n                            if no extension is specified) appended to its\n                            filename.\n\n--backup-always             Create a backup of the original file regardless of\n                            whether or not it's modified.  The backup will have\n                            the extension specified with ``--backup-ext`` (or\n                            ``~`` if no extension is specified) appended to its\n                            filename.\n\n-i EXT, --backup-ext EXT    Create a backup of the input file with ``EXT``\n                            added to the end of the filename.  Implies\n                            ``--backup-changed`` if neither it nor\n                            ``--backup-always`` is also given.\n\n-c, --create                If the input file does not exist, pretend it's\n                            empty instead of erroring, and create it with the\n                            results of the operation.  No backup file will be\n                            created for a nonexistent file, regardless of the\n                            other options.\n\n                            If the input file does not exist and no changes are\n                            made (because ``--backrefs`` was specified and\n                            ``--regexp`` didn't match), the file will not be\n                            created.\n\n-L LINE, --line LINE        Use ``LINE`` as the line to insert.  This option is\n                            useful when ``LINE`` begins with a hyphen.\n\n-m, --match-first           If ``--regexp`` matches, replace the first matching\n                            line with ``line``.\n\n-M, --match-last            If ``--regexp`` matches, replace the last matching\n                            line with ``line``.  This is the default.\n\n-o FILE, --outfile FILE     Write the resulting file contents to ``FILE``\n                            instead of modifying the input file.\n\n                            It is an error to specify this option with any of\n                            ``--backup-changed``, ``--backup-always``, or\n                            ``--backup-ext``.\n\n\n``remove``\n----------\n\n::\n\n    lineinfile remove [\u003coptions\u003e] \u003cregexp\u003e [\u003cfile\u003e]\n    lineinfile remove [\u003coptions\u003e] -e \u003cregexp\u003e [\u003cfile\u003e]\n\nDelete all lines from the given file that match the given `Python regular\nexpression`_.\n\nIf no file name is given on the command line, input is read from standard\ninput, and the result is written to standard output.  It is an error to specify\nany of the ``--backup-changed``, ``--backup-always``, or ``--backup-ext``\noptions when no file is given.\n\nOptions\n```````\n\n--backup, --backup-changed  If the input file is modified, create a backup of\n                            the original file.  The backup will have the\n                            extension specified with ``--backup-ext`` (or ``~``\n                            if no extension is specified) appended to its\n                            filename.\n\n--backup-always             Create a backup of the original file regardless of\n                            whether or not it's modified.  The backup will have\n                            the extension specified with ``--backup-ext`` (or\n                            ``~`` if no extension is specified) appended to its\n                            filename.\n\n-i EXT, --backup-ext EXT    Create a backup of the input file with ``EXT``\n                            added to the end of the filename.  Implies\n                            ``--backup-changed`` if neither it nor\n                            ``--backup-always`` is also given.\n\n-e REGEX, --regexp REGEX    Delete all lines that match ``REGEX``.  This option\n                            is useful when ``REGEX`` begins with a hyphen.\n\n-o FILE, --outfile FILE     Write the resulting file contents to ``FILE``\n                            instead of modifying the input file.\n\n                            It is an error to specify this option with any of\n                            ``--backup-changed``, ``--backup-always``, or\n                            ``--backup-ext``.\n\n\nLibrary API\n===========\n\nNote that all regular expression matching is done with the ``Pattern.search()``\nmethod, i.e., it is not anchored at the start of the line.  In order to force a\nregular expression to start matching at the beginning of a line, prefix it with\n``^`` or ``\\A``.\n\n.. code:: python\n\n    lineinfile.add_line_to_file(\n        filepath: Union[str, bytes, os.PathLike[str], os.PathLike[bytes]],\n        line: str,\n        regexp: Optional[Union[str, re.Pattern[str]]] = None,\n        inserter: Optional[Inserter] = None,\n        match_first: bool = False,\n        backrefs: bool = False,\n        backup: Optional[BackupWhen] = None,\n        backup_ext: Optional[str] = None,\n        create: bool = False,\n        encoding: Optional[str] = None,\n        errors: Optional[str] = None,\n    ) -\u003e bool\n\nAdd the given ``line`` to the file at ``filepath`` if it is not already\npresent.  Returns ``True`` if the file is modified.  If ``regexp`` is set to a\nregular expression (either a string or a compiled pattern object) and it\nmatches any lines in the file, ``line`` will replace the last matching line (or\nthe first matching line, if ``match_first=True``).  If the regular expression\ndoes not match any lines (or no regular expression is specified) and ``line``\nis not found in the file, the line is inserted at the end of the file by\ndefault; this can be changed by passing the appropriate object as the\n``inserter`` argument; see \"Inserters_\" below.\n\nWhen ``backrefs`` is true, if ``regexp`` matches, the capturing groups in the\nregular expression are used to expand any ``\\n``, ``\\g\u003cn\u003e``, or ``\\g\u003cname\u003e``\nbackreferences in ``line``, and the resulting string replaces the matched line\nin the input.  If ``backrefs`` is true and ``regexp`` does not match, the file\nis left unchanged.  It is an error to set ``backrefs`` to true without also\nsetting ``regexp``.\n\nWhen ``backup`` is set to ``lineinfile.CHANGED``, a backup of the file's\noriginal contents is created if the file is modified.  When ``backup`` is set\nto ``lineinfile.ALWAYS``, a backup is always created, regardless of whether the\nfile is modified.  The name of the backup file will be the same as the\noriginal, with the value of ``backup_ext`` (default: ``~``) appended.\n\nIf ``create`` is true and ``filepath`` does not exist, pretend it's empty\ninstead of erroring, and create it with the results of the operation.  No\nbackup file will ever be created for a nonexistent file.  If ``filepath`` does\nnot exist and no changes are made (because ``backrefs`` was set and ``regexp``\ndidn't match), the file will not be created.\n\n\n.. code:: python\n\n    lineinfile.remove_lines_from_file(\n        filepath: Union[str, bytes, os.PathLike[str], os.PathLike[bytes]],\n        regexp: Union[str, re.Pattern[str]],\n        backup: Optional[BackupWhen] = None,\n        backup_ext: Optional[str] = None,\n        encoding: Optional[str] = None,\n        errors: Optional[str] = None,\n    ) -\u003e bool\n\nDelete all lines from the file at ``filepath`` that match the regular\nexpression ``regexp`` (either a string or a compiled pattern object).  Returns\n``True`` if the file is modified.\n\nWhen ``backup`` is set to ``lineinfile.CHANGED``, a backup of the file's\noriginal contents is created if the file is modified.  When ``backup`` is set\nto ``lineinfile.ALWAYS``, a backup is always created, regardless of whether the\nfile is modified.  The name of the backup file will be the same as the\noriginal, with the value of ``backup_ext`` (default: ``~``) appended.\n\n\n.. code:: python\n\n    lineinfile.add_line_to_string(\n        s: str,\n        line: str,\n        regexp: Optional[Union[str, re.Pattern[str]]] = None,\n        inserter: Optional[Inserter] = None,\n        match_first: bool = False,\n        backrefs: bool = False,\n    ) -\u003e str\n\nAdd the given ``line`` to the string ``s`` if it is not already present and\nreturn the result.  If ``regexp`` is set to a regular expression (either a\nstring or a compiled pattern object) and it matches any lines in the input,\n``line`` will replace the last matching line (or the first matching line, if\n``match_first=True``).  If the regular expression does not match any lines (or\nno regular expression is specified) and ``line`` is not found in the input, the\nline is inserted at the end of the input by default; this can be changed by\npassing the appropriate object as the ``inserter`` argument; see \"Inserters_\"\nbelow.\n\nWhen ``backrefs`` is true, if ``regexp`` matches, the capturing groups in the\nregular expression are used to expand any ``\\n``, ``\\g\u003cn\u003e``, or ``\\g\u003cname\u003e``\nbackreferences in ``line``, and the resulting string replaces the matched line\nin the input.  If ``backrefs`` is true and ``regexp`` does not match, the input\nis left unchanged.  It is an error to set ``backrefs`` to true without also\nsetting ``regexp``.\n\n\n.. code:: python\n\n    lineinfile.remove_lines_from_string(\n        s: str,\n        regexp: Union[str, re.Pattern[str]],\n    ) -\u003e str\n\nDelete all lines from the string ``s`` that match the regular expression\n``regexp`` (either a string or a compiled pattern object) and return the\nresult.\n\n\nInserters\n---------\n\nInserters are objects used by the ``add_line_*`` functions to determine the\nlocation at which to insert ``line`` when it is not found in the input and the\n``regexp`` argument, if set, doesn't match any lines.\n\n``lineinfile`` provides the following inserter classes:\n\n``AtBOF()``\n    Always inserts the line at the beginning of the file\n\n``AtEOF()``\n    Always inserts the line at the end of the file\n\n``AfterFirst(regexp)``\n    Inserts the line after the first input line that matches the given regular\n    expression (either a string or a compiled pattern object), or at the end of\n    the file if no line matches.\n\n``AfterLast(regexp)``\n    Inserts the line after the last input line that matches the given regular\n    expression (either a string or a compiled pattern object), or at the end of\n    the file if no line matches.\n\n``BeforeFirst(regexp)``\n    Inserts the line before the first input line that matches the given regular\n    expression (either a string or a compiled pattern object), or at the end of\n    the file if no line matches.\n\n``BeforeLast(regexp)``\n    Inserts the line before the last input line that matches the given regular\n    expression (either a string or a compiled pattern object), or at the end of\n    the file if no line matches.\n\n\nHandling of Line Endings\n========================\n\n``lineinfile`` operates on files using Python's universal newlines mode, in\nwhich all LF (``\\n``), CR LF (``\\r\\n``), and CR (``\\r``) sequences in a file\nare converted to just LF when read into a Python string, and LF is in turn\nconverted to the operating system's native line separator when written back to\ndisk.\n\nIn the majority of cases, this allows you to use ``$`` in regular expressions\nand have it always match the end of an input line, regardless of what line\nending the line had on disk.  However, when using ``add_line_to_string()`` or\n``remove_lines_from_string()`` with a string with non-LF line separators,\nthings can get tricky.  ``lineinfile`` follows the following rules regarding\nline separators:\n\n- Lines are terminated by LF, CR, and CR LF only.\n\n- When an ``add_line_*`` function compares a ``line`` argument against a line\n  in the input, the line ending is stripped from both lines.  This is a\n  deviation from Ansible's behavior, where only the input line is stripped.\n\n- When matching an input line against ``regexp`` or an inserter, line endings\n  are not stripped.  Note that a regex like ``r\"foo$\"`` will not match a line\n  that ends with a non-LF line ending, so this can result in patterns not\n  matching where you might naïvely expect them to match.\n\n- When adding a line to the end of a file, if the file does not end with a line\n  ending already, an LF is appended before adding the line.\n\n- When adding ``line`` to a document (either as a new line or replacing a\n  pre-existing line), LF is appended to the line if it does not already end\n  with a line separator; any line ending on the line being replaced (if any) is\n  ignored (If you want to preserve it, use backrefs).  If the only difference\n  between the resulting ``line`` and the line it's replacing is the line\n  ending, the replacement still occurs, the line ending is modified, and the\n  document is changed.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjwodder%2Flineinfile","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjwodder%2Flineinfile","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjwodder%2Flineinfile/lists"}