【问题标题】:Using sed in bash to replace lines in a xml file在 bash 中使用 sed 替换 xml 文件中的行
【发布时间】:2016-01-20 14:10:17
【问题描述】:

我正在编写一个 bash 脚本来为 hadoop 更改 xml 文件中的一些值。我以前用过一点点 sed,没什么太复杂的。

这是有问题的行:

sed "s@<configuration>@${toedit}@g output.txt" /usr/local/hadooptest/etc/hadoop/core-site.xml

我正在使用 output.txt 来测试输出。完成的脚本会将存储在 $toedit 中的代码插入到:

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. See accompanying LICENSE file.
-->

<!-- Put site-specific property overrides in this file. -->

<configuration>
</configuration>

我想用变量 $toedit 替换 configuration 标签。在 $toedit 我保存了:

toedit='<configuration>
<property>
  <name>hadoop.tmp.dir</name>
  <value>/app/hadoop/tmp</value>
  <description>A base for other temporary directories.</description>
</property>

<property>
  <name>fs.default.name</name>
  <value>hdfs://localhost:54310</value>
</property>'

通过谷歌搜索过去出现的类似问题,我知道我的变量有问题(由于 {} 括号或 '' - 短篇小说是空的)但我不知道为什么!

我尝试过单引号、双引号、无引号、我在网上看到的不同 sed 选项(其中一些我只是按照给定的答案并希望得到最好的结果),但我没有运气。

我认为这有点小,或者有些愚蠢,但我碰壁了!我在整个网站上都看到了 sed 问题,但我发现的选项对我没有帮助。

我应该说这一切都包含在一个 bash 脚本中,最终结果是根据本教程安装和配置 hadoop:http://www.bogotobogo.com/Hadoop/BigData_hadoop_Install_on_ubuntu_single_node_cluster.php

我正在练习脚本,我认为这将是一个很好的挑战!

谢谢

【问题讨论】:

    标签: xml bash variables replace sed


    【解决方案1】:

    sed 不允许在替换文本中出现多个未转义的换行符。您可以使用 awk:

    awk -v toedit="$toedit" '/<configuration>/{print toedit; next} 1' file
    http://www.apache.org/licenses/LICENSE-2.0
    
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License. See accompanying LICENSE file.
    -->
    
    <!-- Put site-specific property overrides in this file. -->
    
    <configuration>
    <property>
      <name>hadoop.tmp.dir</name>
      <value>/app/hadoop/tmp</value>
      <description>A base for other temporary directories.</description>
    </property>
    
    <property>
      <name>fs.default.name</name>
      <value>hdfs://localhost:54310</value>
    </property>
    </configuration>
    

    或使用perl:

    perl -pe "s@<configuration>@$toedit@g" file
    

    【讨论】:

    • 嗨阿努巴瓦!感谢您的回复!他们都在终端窗口中工作得很好,谢谢。我选择了你写的 perl 行。但是,如何将其保存到文件中?我尝试使用 > 它使文件为空白。但是,使用 >> 工作但复制了两次文件。为了澄清,我尝试了 perl -pe "s@@$toedit@g" /usr/local/hadooptest/etc/hadoop/core-site.xml > /usr/local/hadooptest/etc/hadoop/core-site .xml 然后与 >> 相同。此外,perl 是否与大多数 linux 副本捆绑在一起? (只是为了我自己的知识!)再次感谢你:)
    • 要保存更改内联使用:perl -i -pe "s@&lt;configuration&gt;@$toedit@g" file 是的,perl 适用于所有 Linux 发行版。
    【解决方案2】:

    如果 XML 的格式与预期不同,则使用文本处理工具更改 XML 可能会严重破坏 XML 结构。使用适当的 XML 处理工具。比如在xsh:

    open /usr/local/hadooptest/etc/hadoop/core-site.xml ;
    cd //configuration ;
    insert element property into  . ;
    
    cd property ;
    set name        'hadoop.tmp.dir' ;
    set value       'app/hadoop/tmp' ;
    set description 'A base for other temporary directories.' ;
    
    cd .. ;
    insert element property append . ;
    cd property[2] ;
    set name  'fs.default.name' ;
    set value 'hdfs://localhost:54310' ;
    
    xinsert text {"\n"} before ..//* ; # Some basic formatting.
    save :b ;                          # Create a backup.
    

    【讨论】:

      猜你喜欢
      • 2021-01-05
      • 2013-04-30
      • 2010-12-20
      • 2020-09-22
      • 2020-01-17
      • 2017-10-07
      • 1970-01-01
      • 2014-09-08
      • 1970-01-01
      相关资源
      最近更新 更多