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

2019-05-11 05:59发布

问题:

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

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

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.

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 "$1"...

if [ "$1" == 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 [ "$1" == 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 [ "$1" == 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 [ "$1" == 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

回答1:

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 -i -e "s/{VERSION}/${VERSION}/" -e "s/{DISTRO}/${DISTRO}/" ${OUT_CAT}

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

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

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.



回答2:

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_DEBUG that you are piping in?



标签: linux shell sed