Linux 如何使用sed用shell变量替换文件中的字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15230020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 19:11:53  来源:igfitidea点击:

how to use sed to replace a string in a file with a shell variable

linuxshellsed

提问by NlaakALD

Geez, I tried the MAN pages, and several posts here. I guess I'm just stupid because I', not getting it.

天哪,我尝试了 MAN 页面,以及这里的几篇文章。我想我只是愚蠢,因为我不明白。

I have a javascript file that has {VERSION} and {DISTRO} where the variable string values should be replaced like this.

我有一个包含 {VERSION} 和 {DISTRO} 的 javascript 文件,其中应该像这样替换变量字符串值。

var MyObject = {

    /**
     * @property {string} VERSION Holds the current version of the framework
     */
    VERSION:    '{VERSION}',
    /**
     * @property {string} DISTRO Holds the distrobution tag of the framework
     */
    DISTRO:     '{DISTRO}'
};

And this command running for my shell. no matter which whay I do this it does not work or i get errors.

这个命令为我的 shell 运行。无论我做什么,它都不起作用或出现错误。

VERSION="0.9.0"
DISTRO="Developer"
OUT_CAT=$OUT_CAT_DEBUG


${OUT_CAT} | sed -i "s/{VERSION}/$VERSION/" -e "s/{DISTRO}/$DISTRO/" ${OUT_CAT}
#sed -i "s/{VERSION}/$VERSION/" -e "s/{DISTRO}/$DISTRO/" ${OUT_CAT}

So in which way am I being a tard? Seems like it should be very simple and straight forward.

那么我在哪方面是一个迟到者呢?看起来它应该非常简单和直接。

[EDIT] Here is s tripped down version of the script so you can tell what I am doing and where variables are coming from.

[编辑] 这是脚本的绊倒版本,因此您可以知道我在做什么以及变量来自何处。

#!/bin/bash
VERSION="0.9.0"

DEV_DIR=../lib
DIST_DIR=../dist
WEB_DIR=/d/Android_Dev/GitHUB/WEB/h5c3/dist

OUT_CAT_DEBUG=h5c3.debug.cat
OUT_MIN_DEBUG=h5c3.debug.min
OUT_ZIP_DEBUG=$DIST_DIR/h5c3.debug.gz

OUT_CAT_RELEASE=$DIST_DIR/h5c3.release.cat
OUT_MIN_RELEASE=h5c3.release.min
OUT_ZIP_RELEASE=$DIST_DIR/h5c3.release.gz

echo Building Version ""...

if [ "" == debug ]; then
    DISTRO="Developer"
    OUT_CAT=$OUT_CAT_DEBUG
    OUT_MIN=$OUT_MIN_DEBUG
    OUT_ZIP=$OUT_ZIP_DEBUG
    # empty it out
    > ${OUT_CAT}
    cat $DEV_DIR/packed.js >> ${OUT_CAT}
    #Development support
    cat $DEV_DIR/h5c3_debug.js >> ${OUT_CAT}
elif [ "" == release ]; then
    DISTRO="Production"
    OUT_CAT=$OUT_CAT_RELEASE
    OUT_MIN=$OUT_MIN_RELEASE
    OUT_ZIP=$OUT_ZIP_RELEASE
    # empty it out
    > ${OUT_CAT}
    cat $DEV_DIR/packed.js >> ${OUT_CAT}
    cat $DEV_DIR/h5c3_release.js >> ${OUT_CAT}
else
    #publish.sh debug
    #publish.sh release
    exit 2
fi

#Build Game Core
cat $DEV_DIR/core/stacktrace.js >> ${OUT_CAT}

#Build Externals
cat $DEV_DIR/ext/base64.js >> ${OUT_CAT}

#Build Engine
cat $DEV_DIR/engine/boot.js >> ${OUT_CAT} 

#Build Internal Components
cat $DEV_DIR/components/component.js >> ${OUT_CAT}

#Build Systems
cat $DEV_DIR/systems/system.js >> ${OUT_CAT}

#Build Framework
cat $DEV_DIR/framework/page.js >> ${OUT_CAT} 

#Build Web App
cat $DEV_DIR/webapp/game.js >> ${OUT_CAT}

if [ "" == debug ]; then
    #Development support
    cat $DEV_DIR/debugger/profiler.js >> ${OUT_CAT}
    cat $DEV_DIR/debugger/console.js >> ${OUT_CAT}
    cat $DEV_DIR/debugger/debug.js >> ${OUT_CAT}
    cat $DEV_DIR/debugger/developer.js >> ${OUT_CAT}
fi

if [ "" == release ]; then
    echo Removing any debug calls from ${OUT_CAT}
    sed -i '/this.debug/d' ${OUT_CAT}
fi

echo "Inserting distribution & version in ${OUT_CAT}"
${OUT_CAT} | sed -i "s/{VERSION}/$VERSION/" -e "s/{DISTRO}/$DISTRO/" ${OUT_CAT}

echo Minimizing...[${OUT_CAT} to ${OUT_MIN}]
> ${OUT_MIN}
java -jar yuicompressor-2.4.7.jar ${OUT_CAT} -o ${OUT_MIN}

#echo Compressing...[${OUT_MIN} to ${OUT_ZIP}]
#gzip --best --force ${OUT_MIN}

echo Copying to WEB Folder...
cp --update ${OUT_MIN} ${WEB_DIR}

echo Complete.
exit

采纳答案by nlucaroni

It seems to be that you're trying a combination of piping to sed and using a file. The following will work perfectly fine,

您似乎正在尝试将管道连接到 sed 和使用文件的组合。以下将工作得很好,

sed -i -e "s/{VERSION}/${VERSION}/" -e "s/{DISTRO}/${DISTRO}/" ${OUT_CAT}

You don't need to escape the $, and you don't pipe into sedsince you're using -ito modify the file in-place. You also need to use -efor each expression when their are more than one.

您不需要转义$,也不需要管道进入,sed因为您正在使用-i就地修改文件。-e当每个表达式不止一个时,您还需要使用它们。

EDIT:Here is the bit in the sed manual pages that gives the indication of the issue with -eoption (emphasis mine),

编辑:这是 sed 手册页中的一点,它给出了-e选项问题的指示(强调我的),

If no -e, --expression, -f, or --file option is given, then the first non-option argument is taken as the sed script to interpret. All remaining arguments are names of input files; if no input files are specified, then the standard input is read.

如果没有给出 -e、--expression、-f 或 --file 选项,则第一个非选项参数将作为要解释的 sed 脚本。所有剩余的参数都是输入文件的名称;如果未指定输入文件,则读取标准输入。

回答by Chris Seymour

You don't need to escape the $:

你不需要逃避$

$ DISTRO="Developer"

$ VERSION="0.9.0"

$ sed -e "s/{VERSION}/$VERSION/" -e "s/{DISTRO}/$DISTRO/"  file
var MyObject = {

    /**
     * @property {string} VERSION Holds the current version of the framework
     */
    VERSION:    '0.9.0',
    /**
     * @property {string} DISTRO Holds the distrobution tag of the framework
     */
    DISTRO:     'Developer'
};

And what is $OUT_CAT_DEBUGthat you are piping in?

什么是$OUT_CAT_DEBUG你是在管道?