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

前端 未结 5 1040
醉话见心
醉话见心 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:39

    AFAIK, it's not possible. Your best bet will be :

    INPUT FILE

    aaa
    bbb
    ccc
    

    SH SCRIPT

    #!/bin/sh
    
    STRING="${1//\//\\/}"   # using parameter expansion to prevent / collisions
    
    shift
    
    sed "
    s/aaa/$STRING/
    " "$@"
    

    COMMAND LINE

    ./sed.sh "fo/obar" 
    

    OUTPUT

    fo/obar
    bbb
    ccc
    

提交回复
热议问题