gpt4 book ai didi

sed 或 awk 多行替换

转载 作者:行者123 更新时间:2023-12-04 15:27:58 26 4
gpt4 key购买 nike

我正在尝试将格式附加到所有/* TODO : ... */标签,但我在多行区域遇到了问题。我可以做单行 sed;但是对于多行 sed 和 awk,我不知道。

我该怎么做呢?我对两者都持开放态度。
这是我到目前为止所拥有的。

sed 's/\/\/\*[ \t]*TODO[ \t]*:.*/*\//<span style="color:#aaaaaa;font-weight:bold;">&</span>/g'

代替 :
int void main ( int h, char * argv[] )
int a, b; /* TODO :
- include libraries
...
*/
foobar();
/* TODO : fix missing {'s */

和 :
int void main ( int h, char * argv[] )
int a, b; <span style="color:#aaaaaa; font-weight:bold;">/* TODO :
- include libraries
...
*/</span>
foobar();
<span style="color:#aaaaaa; font-weight:bold;">/* TODO : fix missing {'s */ </span>

最佳答案

gawk 'BEGIN{
RS="*/"
replace="<span style=\"color:#aaaaaa; font-weight:bold;\">"
}
/\/\* +TODO/{
gsub(/\/\* +TODO/,replace" /* TODO")
RT=RT "</span>"
}
{ print $0RT}
' file

输出
$ ./shell.sh
int void main ( int h, char * argv[] )
int a, b; <span style="color:#aaaaaa; font-weight:bold;"> /* TODO :
- include libraries
...
*/</span>

foobar();
<span style="color:#aaaaaa; font-weight:bold;"> /* TODO : fix missing {'s */</span>

关于sed 或 awk 多行替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2377092/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com