简体   繁体   中英

How can i except specific lines with sed?

I have this command to run git log and refresh a list of file:

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. The prefix is web/compile/*

I tried to do with this but unfortunately delete eveything whitout /web/compile prefix.

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 . (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. However, double-check your "prefix", if there are leading spaces(indent), you may want to have \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. so \@^\s*/web/compile@ will be easier to read

  • 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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