TDex
TDex

Reputation: 61

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

Upvotes: 1

Views: 167

Answers (1)

Kent
Kent

Reputation: 195079

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

Upvotes: 3

Related Questions