用于自动编辑配置文件的工具

时间:2015-08-02 13:47:02

标签: awk sed vagrant

我使用Vagrant为我们的开发团队配置VM盒。有时我需要在VM上更改服务的端口号或侦听地址,最好是自动更改。

问题:

我正在寻找可以自动编辑文本/配置文件的工具吗?我正在考虑以下命令':

  • 查找以port=开头的行,并将其替换为port=5000
  • 找到与bind 0.0.0.0匹配的行并对该行进行评论。

现在我正在测试sedawk,但有时候这些很痛苦:)

2 个答案:

答案 0 :(得分:1)

昨天我有点无聊并写了一个简单的LineEdit script(GitHub)。这是第一个版本,不完整,我确定有错别字,但对于有人玩的可能会很有趣。

usage: lineedit.py [-h] [-f {starts,regex,exact,ends}] [-n]
                   [-p {replace,above,below,end,begin,comment,delete}]
                   [-m MAX] [-y] [-v] [-l] [--create]
                   file sourceline [destline]

LineEdit: A tool to quickly automate config file editing.

positional arguments:
  file                  Source file: file path which should be edited (should
                        be a textfile).
  sourceline            Source line: line to search for using one of the
                        method described in -f option.
  destline              Destination line: the line which will be added or will
                        replace source line using methods described in -p
                        options. If `-p comment` is used, this string will be
                        the comment characher. (default: None)

optional arguments:
  -h, --help            show this help message and exit
  -f {starts,regex,exact,ends}
                        Find source position using one of the methods.
                        (default: exact)
  -n                    Add the source line to the file, if the source line is
                        not found. Use with `-p end` or `-p begin`. (default:
                        False)
  -p {replace,above,below,end,begin,comment,delete}
                        Destination position: At which position should the
                        destination line be added/replaces. (default: replace)
  -m MAX, --max MAX     Maximum number of matches. If more matches are found,
                        no changes will take place. (default: 1)
  -y                    Yes, make changes to the file. Otherwise only show
                        line numbers which will be changes. (default: False)
  -v                    Output more debug information. (default: False)
  -l, --long-diff       Show whole file with changes, useful if the source
                        file is long. (default: False)
  --create              Create the output file if source file does not exists.
                        Useful if you want to append something to a (new)
                        file. (default: False)

Exit codes are as follows: -1 = There was an error. 0 = Everything went as
intented. 1 = No match was found. Nothing is replaced.

答案 1 :(得分:0)

sed '# port 
     /^port=/ c\
port=5000

     # bind
     s/.*bind 0\.0\.0\.0.*/#&/
     ' YourFile
  • 使用重定向> +临时文件(适用于所有sed版本)或内联-i使用GNU sed修改内容
    • /^port=/ c\行以 port =
    • 开头
    • /.../是使用正则表达式(正则表达式)
    • 的模式选择(过滤器)
    • ^行首的正则表达式字符
    • port=是您的选择
    • c\按下一行更改行(每个结尾\表示添加下一行
  • s/.*bind 0\.0\.0\.0.*/#&/
    • s///用第二个替换第一个模式
    • .*每个角色(所以这里的每一行都是从开始直到绑定第一个和所有内容直到第二个结束)
    • bindbind后跟0.0.0.0.是任何字符的元字符,所以我将它作为litteral字符转义给我们(替代方法是使用类模式[.]
    • #&正在替换为#,后跟找到的带有元字符&的模式(在我们的例子中,整行)