Fill placeholders in file in single pass

前端 未结 5 1749
旧时难觅i
旧时难觅i 2021-02-09 23:11

I have a skeleton text file with placeholder strings:

blah blah blah
blah $PLACEHOLDER_1$
blah
$PLACEHOLDER_2$

and so on. Specific \"form\" of

5条回答
  •  伪装坚强ぢ
    2021-02-09 23:41

    Building on the previous answer, perhaps use an array and compute the sed string?

    #!/bin/sh
    PLACEHOLDER[0]='string 1'
    PLACEHOLDER[1]='multiline 
    string 
    2'
    
    s="sed -i "
    for(( i=0 ; i<${#PLACEHOLDER[*]} ; i++ )) ; do 
        echo ${PLACEHOLDER[$i]}
        s=$s"s/PLACEHOLDER_$i/${PLACEHOLDER[$i]}/g;"
    done
    echo $s
    

    Seems to fail on the multi-line strings, though.

    I don't know how portable Bash arrays might be. Above snippet tested with "GNU bash, version 3.2.17(1)-release (i386-apple-darwin9.0)"

提交回复
热议问题