Manage lines in text files
This module ensures a particular line is in a text file, or can replace an existing line using a regular expression.
It can ensure a particular line is absent from a file.
It can gather a line or lines that match a regular expression from a file for further processing.
This is most useful when only a few lines need changing. Often it is better to template the entire file with the template module.
(line-in-file mode options)mode
The mode to operate in. Should be one of :get, :present or :absent
:getReturn all lines matching the regular expression :regexp from the file. Also returns their line numbers.
:presentEnsure that the passed in :line is in the file optionally placing it in the specified position.
:absentEnsure that any lines maching the regular expression :regexp are not in the file.
options
A hashmap of options. All available option keys and their values are described below
| Option | Description |
|---|---|
:pathrequired truetype string | Path to the file |
:regexpaliases :regextype regexp | The regular expression to look for in every line of the file In In In If the regular expression is not matched, the line will be added to the file. If NOTE: The regular expression is passed as a clojure regex literal or a Java regex but is converted into a sed regular expression for operation on the host. As such the semantics of the regular expression are those of the sed implementation on the host and not of Java's Pattern class. |
:string-matchtype string | A string to look for in every line of the file. In In In The same |
:line-matchtype string | An entire line to look for in every line of the file. In In In The same If neither |
:line-numtype integer | Specify the line to match by line number instead of regular expression. or string |
:linetype string | The contents of the line to insert into the file |
:aftertype regexp | Used with mode If specified, the line will be inserted after the last (or first) match of the specified regular expression. If the first match is required, use If the specified search expression has no matches, the line will be appended to the end (or prepended to the start) of the file. If the prepending to the start is required, use If a match is found for May not be used in conjunction with |
:beforetype regexp | Used with mode If specified, the line will be inserted before the last (or first) match of the specified regular expression. If the first match is required, use If the specified search expression has no matches, the line will be appended to the end (or prepended to the start) of the file. If the prepending to the start is required, use If a match is found for May not be used in conjunction with |
:matchrequired truetype keyword | How to handle files with multiple lines that match. Should be |
:insert-atrequired truetype keyword | If no line matches and no Should be |
(line-in-file :present
{:path "/etc/hosts"
:regexp #"^127\.0\.0\.1"
:line "127.0.0.1 localhost local myhostname"})(line-in-file :present
{:path "/etc/services"
:regexp #"^# http service port"
:line "# http service port"})