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
:get
Return all lines matching the regular expression :regexp
from the file. Also returns their line numbers.
:present
Ensure that the passed in :line
is in the file optionally placing it in the specified position.
:absent
Ensure 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 |
---|---|
:path required true type string | Path to the file |
:regexp aliases :regex type 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-match type string | A string to look for in every line of the file. In In In The same |
:line-match type string | An entire line to look for in every line of the file. In In In The same If neither |
:line-num type integer | Specify the line to match by line number instead of regular expression. or string |
:line type string | The contents of the line to insert into the file |
:after type 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 |
:before type 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 |
:match required true type keyword | How to handle files with multiple lines that match. Should be |
:insert-at required true type 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"})