简体   繁体   English

我如何排除 sed 的特定线路?

[英]How can i except specific lines with sed?

I have this command to run git log and refresh a list of file:我有这个命令来运行 git 日志并刷新文件列表:

sed -E 's|(.*): .*|echo \1: $(git log -1 --pretty="format:%ct" \1)|e' app/config/file.yml

My problem is this command refresh every line in file.yml but i have a prefix which i don't want to refresh.我的问题是此命令刷新 file.yml 中的每一行,但我有一个不想刷新的前缀。 The prefix is web/compile/*前缀是 web/compile/*

I tried to do with this but unfortunately delete eveything whitout /web/compile prefix.我尝试这样做,但不幸的是删除了没有 /web/compile 前缀的所有内容。

sed -i.bkp '/web\/compiled\/*/!e' -E 's|(.*): .*|echo \1: $(git log -1 --pretty="format:%ct" \1)|e' app/config/file.yml
  • First of all, you should know that using sed to modify a yaml file is risky .首先要知道,用sed修改一个yaml的文件是有风险的 (I saw you retain the leading stuff till the last : to avoid interfering the indentations, but it is not safe ) (我看到你把领先的东西保留到最后:避免干扰缩进,但这不安全)

  • using sed, you can match a pattern by /pattern/{action} , here you just fill the pattern part with your "prefix" path.使用 sed,您可以通过/pattern/{action}匹配模式,在这里您只需用“前缀”路径填充pattern部分。 However, double-check your "prefix", if there are leading spaces(indent), you may want to have \s*/web/com....但是,仔细检查你的“前缀”,如果有前导空格(缩进),你可能想要\s*/web/com....

  • similar to your s|pat|rep_or_cmd|e , you can use another separator for the pattern matching, which we talked about in the last item if your pattern contains slash.类似于您s|pat|rep_or_cmd|e ,您可以使用另一个分隔符进行模式匹配,如果您的模式包含斜线,我们在最后一项中讨论过。 so \@^\s*/web/compile@ will be easier to read所以\@^\s*/web/compile@会更容易阅读

  • come to solution.来解决。 You have two different ways to do what you want:你有两种不同的方式来做你想做的事:

     sed '\@^\s*/web/compile@n; s|your s cmd....|' file

OR要么

    sed '\@^\s*/web/compile@!s|your s cmd....|' file

Note笔记

In your question, the prefix you wrote sometimes web/compile sometimes /web/compile , you should write the right one in the sed command and test.在你的问题中,你写的前缀有时是web/compile有时是/web/compile ,你应该在 sed 命令中写正确的并测试。

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

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