Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 将行替换为脚本中带有sed的文件的内容_Bash_Macos_Text_Sed_Replace - Fatal编程技术网

Bash 将行替换为脚本中带有sed的文件的内容

Bash 将行替换为脚本中带有sed的文件的内容,bash,macos,text,sed,replace,Bash,Macos,Text,Sed,Replace,我想使用sed(在MacOs上)将文件中的某些行替换为其他文件的内容 假设文件如下,我想替换包含的所有行 Lorem ipsum dolor sit amet, consectetur adipiscing elit. Paupertas si malum est, mendicus beatus esse nemo <iframe src="https://test.com/bar/11d75d1c627c017299e4fe35e" frameborder=0&g

我想使用sed(在MacOs上)将文件中的某些行替换为其他文件的内容

假设文件如下,我想替换包含
的所有行

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Paupertas si malum est, mendicus beatus esse nemo

<iframe src="https://test.com/bar/11d75d1c627c017299e4fe35e" frameborder=0></iframe>

oportunitatis esse beate vivere. Quasi vero, inquit, perpetua oratio rhetorum solum, non etiam philosophorum

<iframe src="https://test.com/bar/9e4e1b69131bf5d718452aca6" frameborder=0></iframe>

vivere. Quasi vero, inquit, perpetua oratio rhetorum solum, non etiam philosophorum
在哪里

  • $line
    是要更改的行
line=“\\”
  • $replacementFile
    指向其内容必须替换
    $line
    的文件,其内容如下:
LOREM IPSUM DOLOR
阿梅特,康塞特图尔
告别精英。
  • $originalFile
    是必须更改
    $line
    的文件路径
我有几个文件要修改,每个文件都包含几行要更改的内容。因此,我编写了一个脚本,其中所有
类型的行都使用regex找到,然后我应用了在命令行上工作的命令

下面是我写的原始脚本:

函数replaceTextWithFile(){
#要修改的文件(在生产环境中,名称作为参数传递)
本地originalFile=“./file.txt”
#Regex查找要更改的行

local regex=“您的脚本有一些错误:

  • 正则表达式是错误的,它与输入不匹配
  • sed
    使用正则表达式,而不是精确的字符。因此
    /$line/
    将被解析为正则表达式
  • 变量不展开单引号。
    '/$line/'
    按字面意思匹配5个字符
    $line
    。若要展开变量,请使用双引号
  • 当执行已在每行上执行的
    sed
    sed
    时,循环每行是没有意义的
  • bash支持函数func(){
作为支持奇怪语法的扩展,不应使用它。只需定义函数即可
  • 以下命令成功更改第9行
    后接更改第12行的行
  • 因为我要更改的行有一些特定字符
    所以
    在正则表达式中没有特殊意义,但是
    \
    是一个。使用学习正则表达式
  • 要读取输入行,请使用
    IFS=read-r line
  • 只是:

    或者,您甚至可以在
    sed
    中使用不同的正则表达式分隔符,使其更简单:

        local regex='<iframe src="https://.*" frameborder'
        sed "\~$regex~r $replacementFile"
    

    localregex='在
    sed-i'.
    中,
    '
    的目的是什么?
    $line=~$regex然后
    /$line/
    ?不,
    sed
    与regex一起工作,而不是与确切的行一起工作。还有为什么反向引用
    (https…)
    ?您打算匹配
    ?为什么
    https:\/\/foo
    -输入中没有
    foo
    。@lxrx指定备份suffix@KamilCuk,我的意图是,如果行与正则表达式匹配(
    $line=~$regex
    ),然后更改所有行。Sed不适用于精确行?好的,我会朝这个方向搜索,谢谢你
    $line=~$regex),然后更改所有行。
    没有意义。
    Sed
    已经适用于文件中的所有行。就像你在每行上循环行数。我在编写问题时在regex中犯了一个错误。B不过,你的回答@KamilCuk是正确的,解决了我的问题。我也非常感谢所有有趣的建议!谢谢!!
        local regex='<iframe src="https:\/\/.*" frameborder'
    
        local regex='<iframe src="https://.*" frameborder'
        sed "\~$regex~r $replacementFile"