简体   繁体   English

使用sed替换目录中所有文件中的字符串

[英]using sed to replace a string in all files of a directory

I am trying to find and replace all occurances of this: 我正在尝试查找和替换所有这种情况:

 #include <boost

with this: 有了这个:

#include </home/pi/Desktop/boost_1_66_0/boost

for the libraries to work. 为图书馆工作。 here is what i came up with: 这是我想出的:

sed -i 's/#include <boost/#include <\/home\/pi\/Desktop\/boost_1_66_0\/boost/g' *

but gives me this: 但是给我这个:

sed: couldn't edit build: not a regular file sed:无法编辑构建:非常规文件

did i escape the '/' wrong or is # the problem? 我是否避免了'/'错误或#问题?

It looks like build is a directory and you can't run sed on a directory. 看起来build是一个目录,您无法在目录上运行sed One option, if your directory tree is only one deep is to use a final argument */* instead of * . 如果目录树只有一个深层,一种选择是使用最终参数*/*代替* For an arbitrarily deep directory structure a more effective method is to use find to find all the files and run sed on them: 对于任意深度的目录结构,一种更有效的方法是使用find查找所有文件并对其运行sed

find . -type f -exec sed -i 's/from/to/' {} \;

This uses find to start in the current directory and recursively descend the directory tree. 它使用find从当前目录开始,然后递归下降到目录树。 For each regular file ( -type f ) it finds, it will run the command given to the -exec option. 对于找到的每个常规文件( -type f ),它将运行赋予-exec选项的命令。 It substitutes the {} with the files it found. 它将{}替换为找到的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM