How to expand shell variables in a text file?

前端 未结 10 1577
夕颜
夕颜 2021-02-04 02:05

Consider a ASCII text file (lets say it contains code of a non-shell scripting language):

Text_File.msh:

spool on to \'$LOG_FILE_PATH/lo         


        
10条回答
  •  温柔的废话
    2021-02-04 02:45

    Create an ascii file test.txt with the following content:

    Try to replace this ${myTestVariable1} 
    bla bla
    ....
    

    Now create a file “sub.sed” containing variable names, eg

    's,${myTestVariable1},'"${myTestVariable1}"',g;
    s,${myTestVariable2},'"${myTestVariable2}"',g;
    s,${myTestVariable3},'"${myTestVariable3}"',g;
    s,${myTestVariable4},'"${myTestVariable4}"',g'
    

    Open a terminal move to the folder containing test.txt and sub.sed.
    Define the value of the varible to be replaced

    myTestVariable1=SomeNewText
    

    Now call sed to replace that variable

    sed "$(eval echo $(cat sub.sed))" test.txt > test2.txt
    

    The output will be

    $cat test2.txt
    
    Try to replace this SomeNewText 
    bla bla
    ....
    

提交回复
热议问题