使用文件的内容使用SED替换字符串

时间:2021-07-19 09:10:08

What would be the sed command for mac shell scripting that would replace all iterations of string "fox" with the entire string content of myFile.txt.

什么是mac shell脚本的sed命令将用myFile.txt的整个字符串内容替换字符串“fox”的所有迭代。

myFile.txt would be html content with line breaks and all kinds of characters. An example would be

myFile.txt将是包含换行符和各种字符的html内容。一个例子是

    </div>
  </div>
  <br>
  <div id="container2">
    <div class="question" onclick="javascript:show('answer2')";>

Thanks!

谢谢!

EDIT 1

编辑1

This is my actual code:

这是我的实际代码:

sed -i.bkp  '/Q/{
s/Q//g
r /Users/ericbrotto/Desktop/question.txt
}' $file

When I run it I get:

当我运行它时,我得到:

sed in place editing only works for regular files. 

And in my files the Q is replaced by a ton of chinese characters (!). Bizarre!

在我的文件中,Q被大量的中文字符(!)取代。离奇!

2 个解决方案

#1


43  

You can use the r command. When you find a 'fox' in the input...

您可以使用r命令。当你在输入中找到'狐狸'时......

/fox/{

...replace it for nothing...

......一无所获......

    s/fox//g

...and read the input file:

...并读取输入文件:

    r f.html
}

If you have a file such as:

如果你有一个文件,如:

$ cat file.txt
the
quick
brown
fox
jumps
over
the lazy dog
fox dog

the result is:

结果是:

$ sed '/fox/{
    s/fox//g
    r f.html
}' file.txt
the
quick
brown

    </div>
  </div>
  <br>
  <div id="container2">
    <div class="question" onclick="javascript:show('answer2')";>
jumps
over
the lazy dog
 dog
    </div>
  </div>
  <br>
  <div id="container2">
    <div class="question" onclick="javascript:show('answer2')";>

EDIT: to alter the file being processed, just pass the -i flag to sed:

编辑:要更改正在处理的文件,只需将-i标志传递给sed:

sed -i '/fox/{
    s/fox//g
    r f.html
}' file.txt

Some sed versions (such as my own one) require you to pass an extension to the -i flag, which will be the extension of a backup file with the old content of the file:

某些sed版本(例如我自己的版本)要求您将扩展名传递给-i标志,该标志将是包含文件旧内容的备份文件的扩展名:

sed -i.bkp '/fox/{
    s/fox//g
    r f.html
}' file.txt

And here is the same thing as a one liner, which is also compatible with Makefile

这和一个衬垫是一样的,它也与Makefile兼容

sed -i -e '/fox/{r f.html' -e 'd}'

#2


3  

Ultimately what I went with which is a lot simpler than a lot of solutions I found online:

最终我使用的东西比我在网上找到的很多解决方案简单得多:

str=xxxx
sed -e "/$str/r FileB" -e "/$str/d" FileA

Supports templating like so:

像这样支持模板:

str=xxxx
sed -e "/$str/r $fileToInsert" -e "/$str/d" $fileToModify

#1


43  

You can use the r command. When you find a 'fox' in the input...

您可以使用r命令。当你在输入中找到'狐狸'时......

/fox/{

...replace it for nothing...

......一无所获......

    s/fox//g

...and read the input file:

...并读取输入文件:

    r f.html
}

If you have a file such as:

如果你有一个文件,如:

$ cat file.txt
the
quick
brown
fox
jumps
over
the lazy dog
fox dog

the result is:

结果是:

$ sed '/fox/{
    s/fox//g
    r f.html
}' file.txt
the
quick
brown

    </div>
  </div>
  <br>
  <div id="container2">
    <div class="question" onclick="javascript:show('answer2')";>
jumps
over
the lazy dog
 dog
    </div>
  </div>
  <br>
  <div id="container2">
    <div class="question" onclick="javascript:show('answer2')";>

EDIT: to alter the file being processed, just pass the -i flag to sed:

编辑:要更改正在处理的文件,只需将-i标志传递给sed:

sed -i '/fox/{
    s/fox//g
    r f.html
}' file.txt

Some sed versions (such as my own one) require you to pass an extension to the -i flag, which will be the extension of a backup file with the old content of the file:

某些sed版本(例如我自己的版本)要求您将扩展名传递给-i标志,该标志将是包含文件旧内容的备份文件的扩展名:

sed -i.bkp '/fox/{
    s/fox//g
    r f.html
}' file.txt

And here is the same thing as a one liner, which is also compatible with Makefile

这和一个衬垫是一样的,它也与Makefile兼容

sed -i -e '/fox/{r f.html' -e 'd}'

#2


3  

Ultimately what I went with which is a lot simpler than a lot of solutions I found online:

最终我使用的东西比我在网上找到的很多解决方案简单得多:

str=xxxx
sed -e "/$str/r FileB" -e "/$str/d" FileA

Supports templating like so:

像这样支持模板:

str=xxxx
sed -e "/$str/r $fileToInsert" -e "/$str/d" $fileToModify