linux - 庆典 : inexplicable behavior sed

标签 linux bash shell awk sed

我写了一个非常简单的 bash 脚本:

#!/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

执行返回:

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>

永远不会应用修改。文件可以修改..

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

最佳答案

你的整个脚本应该重写为这个命令:

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

在某些 seds 中,您需要为 -i 提供一个参数。

关于linux - 庆典 : inexplicable behavior sed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32863518/

相关文章:

linux - 如何知道 Linux/Plesk Vps 有多少 RAM 和多少 CPU 可用?

linux - 在远程计算机上运行脚本时如何在我的计算机上发出 "beep"?

linux - 尝试使用 cp 将文件复制到多个文件时出错

linux - 从 shell 运行 fortran 代码不起作用

linux - 无法使用查找实用程序找到文件

c - 什么是 C 的高效工作流程? - 生成文件 + bash 脚本

bash - Bash中单方括号和双方括号的区别

bash - 使命令的输出显示在单行上

shell - 使用 shell 脚本解析数据。 (排序/uniq/grep)

bash - 为什么在 Bash 中我的条件会出现 "unexpected operator"错误?