庆典:无法解释的行为SED [英] bash : inexplicable behavior sed

查看:73
本文介绍了庆典:无法解释的行为SED的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个很简单的bash脚本如下:

 #!/斌/庆典n线= $(awk的'/ text_to_parse / {打印NR;出口}'te​​stFile.xml)
回声LINE = $ n线
SED -e$ {n线}等/.*/新的文/< testFile.xml
回声
猫testFile.xml退出0

执行返回:

 行= 8
<试验>
    <名称>首先测试与LT; /名称>
    <版本> 1.0 LT; /版本>
    <指挥与GT;新文字< /指挥GT;
< /试验><试验>
    <名称>首先测试与LT; /名称>
    <版本> 1.0 LT; /版本>
    <指挥与GT; text_to_parse< /指挥GT;
< /试验>

的修改是永远不会应用。该文件可以进行修改。

  -rwxrwxrwx 1根根290 1月1日00:23 testFile.xml


解决方案

您整个脚本应该被改写为这一个命令:

  SED -i的/.* text_to_parse。* /新文本/'testFile.xml
猫testFile.xml

您将需要在某些SEDS提供一个参数,以 -i

I wrote a quite simple bash script as :

#!/bin/bash

nLine=$(awk '/text_to_parse/{ print NR; exit }' testFile.xml)
echo "LINE = $nLine"
sed -e "${nLine}s/.*/new text/" < testFile.xml
echo 
cat testFile.xml

exit 0

the execution returns :

LINE = 8
<Test>
    <Name>First Test</Name>
    <Version>1.0</Version>        
    <Command>new text</Command>
</Test>

<Test>
    <Name>First Test</Name>
    <Version>1.0</Version>        
    <Command>text_to_parse</Command>
</Test>

The modification is never applied. The file can be modified ..

 -rwxrwxrwx 1 root root  290 Jan  1 00:23 testFile.xml

解决方案

Your whole script should be rewritten as this one command:

sed -i 's/.*text_to_parse.*/new text/' testFile.xml
cat testFile.xml

You'll need to provide a param to -i in some seds.

这篇关于庆典:无法解释的行为SED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆