Given the following file.txt
:
this is line 1
# this is line 2
this is line 3
I would like to use sed to replace the lines with #
at the beginning with \e[31m${lineContent}\e[0m
. This will color that particular line. Additionally, I need the color \e[31m
to be in a variable, color
. (The desired output of this example would be having the second line colored). I have the following:
function colorLine() {
cat file.txt | sed ''/"$1"/s//"$(printf \e[31m $1 \e[0m)"/g''
}
colorLine "#.*"
The variable color
is not included in what I have so far, as I am not sure how to go about that part.
The output of this is (with the second line being red):
this is line 1
#.*
this is line 3
It is apparently interpreting the replace string literally. My question is how do I use the matched line to generate the replace string?
I understand that I could do something much easier like appending \e[31m
to the beginning of all lines that start with #
, but it is important to use sed with the regexes.