简体   繁体   English

如何用引号替换尖括号

[英]How to replace angle brackets with quotation marks

I have a lot of source codes need modifying, simply as from我有很多源代码需要修改,就像 from

#include <headerA.h>

to

#include "headerA.h"

I've tried some sed, awk commands but not completely sure how to proceed this.我尝试了一些 sed、awk 命令,但不完全确定如何进行此操作。 I'm working on Ubuntu platform.我正在 Ubuntu 平台上工作。 Any inputs would be greatly appreciated!任何输入将不胜感激!

This will do it:这将做到:

sed -i '/^#include/s/[<>]/"/g' filename

The /^#include/ address at the beginning tells sed to perform the substitution only on lines that begin with #include .开头的/^#include/地址告诉sed仅对以#include开头的行执行替换。 [<>] is a regular expression that matches eith < or > , they get replaced with " , and the g modifier tells it to replace all the occurrences on the line, not just the first. [<>]是一个匹配<>的正则表达式,它们被替换为" ,并且g修饰符告诉它替换行上的所有出现,而不仅仅是第一个。

First use find with sed and then use mv首先使用findsed然后使用mv

find . -name <ol_file_nme> -exec sed 's/[<>]/"/g' '{}' \; -print > <new_file_name>
mv  <new_file_name> <ol_file_nme>

This should edit each file in place:这应该就地编辑每个文件:

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

This might work for you:这可能对你有用:

sed '/#include/y/<>/""/' file

Focus on lines that contain headers and translate ( y/.../.../ ) the required characters.关注包含标题的行并翻译 ( y/.../.../ ) 所需的字符。

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

will change:将改变:

  • any file with extension .h or .c under /Your/Source/Path /Your/Source/Path下扩展名为.h.c任何文件
  • any surrounding text by <> with same text surrouonding by double quote <>任何周围文本,以及由双引号包围的相同文本
    • on line starting by #include在线以#include开头
sed -i -E "s/^(#include )[\<]([^\>]+)[\>]/\1\\\"\2\\\"/g" sourcefile.c

or, as we don't need variable substitution inside the rule, with single quotes:或者,因为我们不需要规则内的变量替换,所以使用单引号:

sed -i -E 's/^(#include )[\<]([^\>]+)[\>]$/\1"\2"/g'

Loop through all subdirectories of the working directory looking for *.c and *.h files and replace the angle brackets with quotation marks:循环遍历工作目录的所有子目录以查找 *.c 和 *.h 文件,并用引号替换尖括号:

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

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

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