I am trying to append formatting to all /* TODO : ... */ tags, but I am having trouble in the multi-line area. I can do single line sed's; but for multiline sed and awk, I don't know.
How do I do this? I'm open to either. Here's what I have so far.
sed 's/\/\/\*[ \t]*TODO[ \t]*:.*/*\//<span style="color:#aaaaaa;font-weight:bold;">&</span>/g'
replace :
int void main ( int h, char * argv[] )
int a, b; /* TODO :
- include libraries
...
*/
foobar();
/* TODO : fix missing {'s */
with :
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>
By default, when sed reads a line in the pattern space, it discards the terminating newline (\n) character. Nevertheless, we can handle multi-line strings by doing nested reads for every newline.
I find awk much faster than sed . You can speed up grep if you don't need real regular expressions but only simple fixed strings (option -F). If you want to use grep, sed, awk together in pipes, then I would place the grep command first if possible.
awk has two functions; sub and gsub that we can use to perform substitutions. sub and gsub are mostly identical for the most part, but sub will only replace the first occurrence of a string. On the other hand, gsub will replace all occurrences. Let's take a closer look at how we can make substitutions using awk.
gawk 'BEGIN{
RS="*/"
replace="<span style=\"color:#aaaaaa; font-weight:bold;\">"
}
/\/\* +TODO/{
gsub(/\/\* +TODO/,replace" /* TODO")
RT=RT "</span>"
}
{ print $0RT}
' file
output
$ ./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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With