Using variables in sed -f (where sed script is in a file rather than inline)

前端 未结 5 1165
醉话见心
醉话见心 2021-01-13 03:01

We have a process which can use a file containing sed commands to alter piped input.

I need to replace a placeholder in the input with a variable value,

5条回答
  •  天命终不由人
    2021-01-13 03:56

    This might work for you (GNU sed):

    cat <<\! > replacements.sed
    /XX/{s//'"$(date +%F)"'/;s/.*/echo '&'/e}
    !
    echo "Today is XX" | sed -f replacements.sed
    

    If you don't have GNU sed, try:

    cat <<\! > replacements.sed
    /XX/{
        s//'"$(date +%F)"'/
        s/.*/echo '&'/
    }
    !
    echo "Today is XX" | sed -f replacements.sed | sh
    

提交回复
热议问题