简体   繁体   English

在bash脚本中的冒号后查找并替换值

[英]Find And Replace Value after colon in bash script

I have a file like this 我有一个这样的文件

parameters:
    database_host: 127.0.0.1
    database_port: null
    database_name: myDb
    database_user: root
    database_password: null
    mailer_transport: smtp
    mailer_host: 127.0.0.1
    mailer_user: null
    mailer_password: null
    secret: ea64f518be08e0d5895335990f10d984c22f400c
    name_db: newDB

This is a yml file, and it has a particular format 这是yml文件,并且具有特定格式

I Want to create a file sh that find and replace the value after name_db with a parameter 我想创建一个文件sh,该文件查找并用参数替换name_db之后的值

Look for the key ( name_db: ) and replace the value after it. 查找键( name_db:并替换其后的值。 Assuming a sed that supports the -i option: 假设sed支持-i选项:

sed -i.bak "/^[[:space:]]*name_db:/ s/:.*/: $newname/" file.yml

The regular expression /^[[:space:]]*name_db:/ looks for a line that starts with zero or more spaces followed by name_db: . 正则表达式/^[[:space:]]*name_db:/查找以零个或多个空格开头的行,后跟name_db: When that line is found, the s/:.*/: $newname/ substitute is executed. 找到该行后,将执行s/:.*/: $newname/替换。 It will replace the colon and whatever follows with colon, space, and the value in $newname . 它将替换冒号及其后的冒号,空格和$newname的值。 This will fail if $newname contains a slash. 如果$newname包含斜杠,则此操作将失败。 If that's a possibility, choose a different character (other than / ) as the marker. 如果可能,请选择其他字符( /除外)作为标记。 In case of doubt, Control-A is quite useful and unlikely to be part of a valid name_db value. 如有疑问, Control-A非常有用,并且不太可能成为有效的name_db值的一部分。

This doesn't care what the old value is. 这不在乎旧值是多少。 You can simply wrap this whole expression in double quotes, which interpolates the value in $newname . 您可以简单地将整个表达式用双引号引起来,这$newname的值插入。 Nevertheless, using single quotes around sed scripts is generally a good idea. 但是,在sed脚本周围使用单引号通常是一个好主意。


monkeyUser commented: monkeyUser评论:

sed: 1: "file.yml": extra characters at the end of p command

Note that the command line shown (using -i.bak ) works on both Linux (GNU sed ) and Mac OS X (BSD sed ). 请注意,所示的命令行(使用-i.bak )在Linux(GNU sed )和Mac OS X(BSD sed )上均可使用。 GNU sed allows an optional backup suffix which must be attached to the -i option if it is present; GNU sed允许一个可选的备份后缀,如果存在,该后缀必须附加到-i选项中。 BSD sed requires a suffix which must either be attached to the -i option as shown or can be the next argument. BSD sed需要一个后缀,该后缀必须如图所示附加到-i选项,或者可以是下一个参数。 If you want no backup with GNU sed , specify just -i … ; 如果您不希望使用GNU sed备份,请指定-i … ; with BSD sed , specify '' as an empty argument after the -i option: -i '' … . 对于BSD sed ,在-i选项之后将''指定为空参数: -i '' … Given the error message sed: 1: "file.yml": extra characters at the end of p command , I'm suspicious that the code was run on a system with BSD sed , though I'm not quite sure why p was mentioned (I suspect some editing of the actual error message). 给定错误消息sed: 1: "file.yml": extra characters at the end of p command ,我怀疑代码是在具有BSD sed的系统上运行的,尽管我不太确定为什么提到了p (我怀疑对实际错误消息进行了一些编辑)。 That is: sed -i /something/ file.yml would, with BSD sed , treat /something/ as the backup file suffix and then find problems with file.yml treated as a sed script…except I think the file name began with p rather than f , since f isn't a sed command (but p is). 也就是说: sed -i /something/ file.yml将与BSD sed ,将/something/作为备份文件后缀,然后将file.yml作为sed脚本来查找问题……除了我认为文件名以p开头而不是f ,因为f不是sed命令(但p是)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM