Color in sed


Recommended Posts

Hii,

 

I am stuck with a problem.. 

 

There is a file with the following content

abc.scr
100
101
102
xyz.scr
103
104
105
pqr.scr
106
107
108

I want to change the color of all the lines ending with scr to be red / anything. And then email the file. 

With some googling, I was able to make up with this:

cat sedtest | sed ''/^[a-z]/s//`printf "\033[32m^[a-z]\033[0m"`/''

But the output is: 

^[a-z]bc.scr
100
101
102
^[a-z]yz.scr
103
104
105
^[a-z]qr.scr
106
107
108

How can I achieve the required output using sed? or anything.

TIA !!

Link to comment
Share on other sites

cat sedtest | sed -r "s/(^[a-Z]+.scr)/`printf "\033[31m"`\1`printf "\033[0m"`/g"

(...) is used for grouping expressions in the regex. \1 is used for printing the first group in the output (there is only one group in this case). printf's surround the \1 to print the color code command for red and the escape color code command. -r is for extended regex (in order to use the +).

 

Note: due to the (...) you must use " (double quote) and not '' (two single quotes) here.

Link to comment
Share on other sites

cat sedtest | sed -r "s/(^[a-Z]+.scr)/`printf "\033[31m"`\1`printf "\033[0m"`/g"

(...) is used for grouping expressions in the regex. \1 is used for printing the first group in the output (there is only one group in this case). printf's surround the \1 to print the color code command for red and the escape color code command. -r is for extended regex (in order to use the +).

 

Note: due to the (...) you must use " (double quote) and not '' (two single quotes) here.

 

Wow !!, that is just great.  Though I am able to get this work on terminal, not able to get the same output via email

cat sedtest | sed -r "s/(^[a-Z]+.scr)/`printf "\033[31m"`\1`printf "\033[0m"`/g" > sedtest1

Any recommendations?

Link to comment
Share on other sites

I'm confused as to what you mean by email, I thought you were just attaching the file and emailing that to someone to cat on terminal. What are you trying to do?

Link to comment
Share on other sites

cat /tmp/accids | sed -r "s/(^[a-Z]+.scr)/`printf "\033[31m"`\1`printf "\033[0m"`/g" > /tmp/accids


mutt -a /tmp/accids -s " Account IDs" -- abcd@gmail.com

Above is what I want to do.

 

I want to change the formatting of the file and email it to my gmail account.

Link to comment
Share on other sites

What neoraptor said. It would be simple to change the console color tags into html color tags, but you are going to have to get mutt to send html emails and you are probably going to need to properly format the html email.

Link to comment
Share on other sites

What neoraptor said. It would be simple to change the console color tags into html color tags, but you are going to have to get mutt to send html emails and you are probably going to need to properly format the html email.

 

hmm..I am getting what you people are trying to convey.  Thanks !! 

 

Any documentation that will help me to achieve this i.e convert my file to HTML and then email

 

TIA

Link to comment
Share on other sites

 

Well, thank you very much for your response.  I thought tput or anything related will help me to send the email in color.  I think I need to scratch my head a bit to send html files.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.