I'm curious why you chose sed.
The basic trouble is that you want to change the output, based upon the input. I.e. you have 2 different conditions to look for.
Sed only makes one pass through the file, the "Change To" pattern cannot change during the run.
You need a bit of intelligence to interpret the line and act accordingly. For convenience, you could still use sed to get the initial lines, and awk to output them. Or, awk can do the whole thing.
Ex:
Code:
sed -n -E /${ptrn}/,/^[[:blank:]]*$/p | awk ...
Let's start with this idea.
You can pass variable to awk with the -v option:
Code:
awk -v blu="$blu" -v wht=$wht ...
awk command are enclosed in '{}':
Code:
cat <my_file> | awk '{print $0}'
Or, in this case, a better example would be:
Code:
awk '{print $0}' <my_file>
Inside awk, the arguments $0, $1, $2 represent the input. $0 is the input line, unchanged. $1 is the first 'word', $2 the second, etc. Where a word is split from the input line based on the delimiter. By default it is white space. In your case, I would guess you are only interested in $0.
You can perform pattern matching inside awk. You say your regex is:
Code:
[[:blank:]]*(##|;;|!!|//|@c)[[:blank:]]+
First, confirm that this the correct type of regex for your version of awk (could be awk, gawk, nawk, etc.)
If command in awk are nearly the same as any other language:
Code:
if ($0 ~ /<regex>/) {print blu $0 wht} else {print $0}
Note: the tilde (~) means match the pattern. The slashes (/) represent a pattern, not a literal string. Not match is !~.
To match a literal string, use the double quote (") and double equal (=):
Code:
if ( $0 == "Hello) ...
Not equal would be !=
Important: Read the man pages for awk! There is a lot to it. Esp look at the -v -F and -f options. And the if command, expressions and print commands.
Yo can also make an "awk script" (see the -f option) that gives you a bit more flexibility. With this, you could, conceivably do the entire task in awk.
Code:
{
if ($0 ~ /start_ptrn/) {
print_flag=1
...your color matching stuff here ...
if (print_flag=1) ...print my output here...
if ($0 ~ /end_ptrn/) print_flag=0
}
If you choose this option, definitely do it in an awk script it will be so much easier to maintain and debug.
Remember, read the man page! I've only outlined a rough template, no guarantees that it will work as I've written it. Hopefully it is clear enough to get you started.