通过 sed 命令在 bash 脚本中编辑文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18079317/
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-09-18 06:08:50  来源:igfitidea点击:

Edit File in bash script by sed command

bashscriptingsed

提问by Yasmin

I am a beginner in bash script, and I am facing a problem. I created file "script.sh" and I want to open template file and replace specific attributes with different values. Here is my script and I appreciate if someone tell me what is the problem ..

我是 bash 脚本的初学者,我正面临一个问题。我创建了文件“script.sh”,我想打开模板文件并用不同的值替换特定的属性。这是我的脚本,如果有人告诉我有什么问题,我将不胜感激..

#!/bin/bash
TEMPLATEFILE="Template.xml"
XMLDir="Results"
OUTPUTDir="Output"
TIMES=2 
QUAN=50
DIST=150
OUTPUT_File="Output_"
mkdir  $XMLDir      
mkdir  $OUTPUTDir   
rm -rf $XMLDir      
rm -rf $OUTPUTDir   
echo $TIMES
x=1
echo ${x}
while [ $x -le $TIMES ]
do
    RANDOMVAR =${Random}
    outputFile=${OUTPUT_File}"_"${QUAN}"_"${RANDOMVAR}"_"${DIST}".xml"
    outputText=${OUTPUT_File}"_"${QUAN}"_"${RANDOMVAR}"_"${DIST}".txt"
    touch ${outputFile}  
        sed -e "s/$SEEDVALUE/$RANDOMVAR/"
            -e "s/$EXPIREMENT/$QUAN/"
            -e "s/$DISTANCE/$DIST/"
            -e "s/$OUTPUTFILE/$outputText/"
            < $TEMPLATEFILE > $outputFile
    echo $outputFile" is generated in "$x" iteration out of "$TIMES
    x=$(( $x + 1 ))
done
echo "done"

I run the file in terminal by : bash script.sh

我通过以下方式在终端中运行文件:bash script.sh

The xml file contains a lot of lines, but I am trying to change the following:

xml 文件包含很多行,但我正在尝试更改以下内容:

<run length="0" seed_value="SEEDVALUE" />
<outputfileName name="expirement" output="OUTPUTFILE" />
<distancevalue value="DISTANCE" />
<entity number="EXPIREMENT" base_num="1">

When running the script , it gives me "unexpected end of file"

运行脚本时,它给了我“意外的文件结尾”

回答by dtorgo

sed -e "s/$SEEDVALUE/$RANDOMVAR/"

sed -e "s/$SEEDVALUE/$RANDOMVAR/"

This command is taking a variable "$SEEDVALUE" and replacing it with "$RANDOMVAR" but you are never setting the SEEDVALUE to anything. The actual command that you are issuing looks like this:

此命令采用变量“$SEEDVALUE”并将其替换为“$RANDOMVAR”,但您永远不会将 SEEDVALUE 设置为任何内容。您发出的实际命令如下所示:

sed -e "s//$RANDOMVAR/"

sed -e "s//$RANDOMVAR/"

For my string replace placeholders I usually make them stand out by using "handlebars" {{ }}.

对于我的字符串替换占位符,我通常使用“把手”{{}} 使它们脱颖而出。

Try this instead:

试试这个:

First make sure that your template file contains the placeholder {{SEEDVALUE}}. Second, you do not need to do the first redirect "<" sed will act on a file if you simply pass it in.

首先确保您的模板文件包含占位符 {{SEEDVALUE}}。其次,你不需要做第一次重定向,如果你简单地传入它,sed 将作用于一个文件。

Here is a VERY simplified version to help make things clearer:

这是一个非常简化的版本,以帮助使事情更清晰:

echo "This is some line. This is what was replaced with my placeholder: {{SEEDVALUE}}" > /tmp/testfile

RANDOMVAR="my new text"

sed -e "s/{{SEEDVALUE}}/$RANDOMVAR/" /tmp/testfile > /tmp/testfile-replaced

cat /tmp/testfile-replaced

回答by StianE

Looks to me like those sed commands arguments are not on the same line and line shift is not escaped. Try:

在我看来,那些 sed 命令参数不在同一行上,并且不会对换行进行转义。尝试:

sed -e "s/$SEEDVALUE/$RANDOMVAR/" \
    -e "s/$EXPIREMENT/$QUAN/" \
    -e "s/$DISTANCE/$DIST/" \
    -e "s/$OUTPUTFILE/$outputText/" \
    < $TEMPLATEFILE > $outputFile

Also, to get more debugging information, it's often useful to use the following as interpreter:

此外,为了获得更多调试信息,使用以下内容作为解释器通常很有用:

#!/bin/bash -x

回答by Piervit

Not sure what you are doing. I don't think $SEEDVALUE variable is defined.

不确定你在做什么。我认为没有定义 $SEEDVALUE 变量。

I think your sed command should start a bit more like this

我认为你的 sed 命令应该像这样开始

sed -e "s/seed_value=\"[0-9]*\"/seed_value=\"$RANDOMVAR\"/g"