Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.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
Linux 如何用引号替换尖括号_Linux_String_Replace_Awk_Sed - Fatal编程技术网

Linux 如何用引号替换尖括号

Linux 如何用引号替换尖括号,linux,string,replace,awk,sed,Linux,String,Replace,Awk,Sed,我有很多源代码需要修改,只是从 #include <headerA.h> 我尝试了一些sed、awk命令,但不完全确定如何继续。我在Ubuntu平台上工作。任何意见都将不胜感激 这样就可以了: sed -i '/^#include/s/[<>]/"/g' filename 开头的/^include/地址告诉sed仅对以include开头的行执行替换。[]是一个与之匹配的正则表达式,它们被替换为,并且g修饰符告诉它替换行中的所有引用,而不仅仅是第一个引用。首先使用sed

我有很多源代码需要修改,只是从

#include <headerA.h>
我尝试了一些sed、awk命令,但不完全确定如何继续。我在Ubuntu平台上工作。任何意见都将不胜感激

这样就可以了:

sed -i '/^#include/s/[<>]/"/g' filename
开头的/^include/地址告诉sed仅对以include开头的行执行替换。[]是一个与之匹配的正则表达式,它们被替换为,并且g修饰符告诉它替换行中的所有引用,而不仅仅是第一个引用。

首先使用sed查找,然后使用mv


这将在位编辑每个文件:

find . -name '*.[ch]' -exec \
    sed -i 's|^#include[[:blank:]]\{1,\}<\([^>]\{1,\}\)>[[:blank:]]*$|#include "\1"|' '{}' \;

这可能适合您:

sed '/#include/y/<>/""/' file
关注包含标题的行,并将y/../…/转换为所需的字符

find /Your/Source/Path \
 -name '*.[ch]' \
 -exec \
    sed -i '/^#include[[:blank:]]/ s/<\([^>]*\)>/"\1"/g' "{}" \
 \;
将改变:

/Your/Source/Path下任何扩展名为.h或.c的文件 用双引号括起来的相同文本包围的任何文本 从include开始的联机 或者,由于我们不需要在规则内部使用单引号进行变量替换:

sed -i -E 's/^(#include )[\<]([^\>]+)[\>]$/\1"\2"/g'
在工作目录的所有子目录中循环查找*.c和*.h文件,并用引号替换尖括号:

find . \( -name "*.c" -o -name "*.h" \) -print0 | xargs -n1 -0 sed -i -E 's/^(#include )[\<]([^\>]+)[\>]$/\1"\2"/g'

el pluse uno使用find,但请注意@barmar关于字符其他用法的明智警告和解决方案;-祝大家好运。只需一句话,您在evenutal信息的末尾使用了。*$,以后不再引用。是的,我应该在此处使用[[:blank:][]*来完全正确。我将编辑答案。
sed -i -E "s/^(#include )[\<]([^\>]+)[\>]/\1\\\"\2\\\"/g" sourcefile.c
sed -i -E 's/^(#include )[\<]([^\>]+)[\>]$/\1"\2"/g'
find . \( -name "*.c" -o -name "*.h" \) -print0 | xargs -n1 -0 sed -i -E 's/^(#include )[\<]([^\>]+)[\>]$/\1"\2"/g'