39

I need to save the result of a grep command into a file, but I also want the output file to be formatted and keep the colors just like in the terminal.

Is there a way to do that? Maybe make grep save to a some kind of markup language? If it is not possible, is there another tool that can accomplish this task?

I am trying to make the search keyword stand out in the output file, exactly like it does in the terminal.

4 Answers 4

48

To keep the markup, you need more than just a text file. I'd use HTML output to keep the colors.

  • Install aha i.e. to "converts ANSI escape sequences of a unix terminal to HTML code"

    sudo apt-get install aha
    
  • then save your grep (or ls) output like this:

    ls --color=always | aha --black --title 'ls-with-colors' > ls-with-colors.html
    

    options:

    --black param to set background as black color
    --title param to set the title for HTML page
    

Syntax highlight for file extension via pygmentize

  • install pygmentize:

    sudo apt-get install python-pygments
    
  • then run this kind of command:

    pygmentize file.pl | grep -i --color=always version | aha --black > ls-with-colors.html
    

enter image description here

2
  • 1
    AHA!!, aha is a really wonderful tool, exactly what I was looking for, Thanks !! Jul 25, 2014 at 9:46
  • 2
    @Mhd.Tahawi: I've updated my answer so that you can keep the code colors (perl in your case), but pygmentize works with many others. Jul 25, 2014 at 9:47
11

Depending on what you're wanting to do with the output file, it's possible to add colors to a normal text file because the colors simply come from some special characters. Grep seems to not want to print them when you redirect it to a file, so you need to force it to:

grep --color=always "stuff" input.txt > output.txt

Now, when you print the file to the console it will be printed with the colors, because Bash interprets those characters as "use this color".

cat output.txt

However, if you open it in an editor like vim, you'll get some strange characters. For example, when I use the commands

echo "A sentence. A red sentence. Another sentence." \
  | grep --color=always "A red sentence" > output.txt

The output looks right when I print it using cat but when I open it in vim I get

A sentence. ^[[01;31m^[[KA red sentence^[[m^[[K. Another sentence.

So if you're wanting to use an editor this probably isn't what you want.

3
  • Correct !!, I was getting those strange symbols in between and didn't understand what are those. Jul 26, 2014 at 7:55
  • i really like how simple/effective your answer is. just cat the output :). this is great for short term (just good enough for me right now). Feb 22, 2017 at 21:12
  • Detail: It's the terminal (emulator), not bash, that interprets the color escape sequences (aka strange characters).
    – masterxilo
    May 3, 2018 at 15:46
2

If I understood correctly you want to save a terminal output in a text file, right? But you want it to be formatted with colors. If that's the case, here are my ideas:

Highlighting the output automatically

As you probably know, if you capture a grep output into a text file, exactly because it is a text file it cannot be formated. So, as far as I know, you cannot do it in an easy way.

In spite of that there is a simple workaround, consisting in making realize your text editor what kind of file is opening. For example, let's say that your grep output have some bash components, so the bash highlights works for you (by the way, these are often the colors that you see in a colored output in the terminal). So the trick is to save the text output in a file with the proper extension. Instead of doing something like:

ls | grep something > output

you may go for

ls | grep something > output.sh

Which will make gedit (or any decent text editor) automatically recognize that you're talking about bash code, and will highlight it accordingly. You don't need to color the output, the program will do it for you if it recognizes the type of code that it's opening. If you are working with other type of formats, just adapt the extension to that adjusting better for what you are greping on (e.g. > output.xml, > output.html , > output.py ...etc). Good luck! :)

Highlighting the some words in the output file

So, if I got it, you want to highlight the words you searched for. Again, that cannot be done in a plain text file just because is a plain text. However you can add some format to it in a very easy way such as using some html coding. This will transform your output in an html code, and when you open it with a program able to interpret html (libreoffice writer, firefox, and 10000 etceteras) you will see some words highlited.

To do so, let's say this is your grep, exported to html:

ls | grep keytext > output.html

And now you want to highlight keytext in your output. You can use sed to do it, like:

sed -i 's/keytext/<font color="red">keytext<\/font>/g' output.html

And violà, now your keytext is highlited in red.

4
  • 1
    nice idea !!, but I am not looking for syntax highlighting. you know how grep color the search keyword with another color, usually red. I want to keep that so the keyword standout in the output file. Jul 25, 2014 at 9:27
  • ok, so, you are greping something with color? like an html code? If you see color in the input then it have to be formated and hence coded somewhere, like "fontcolor:red", right? If this is the case, you may need to include the formating code in your grep. :)
    – Rho
    Jul 25, 2014 at 9:33
  • ok, with the edit in the question I get it. :) What output extension are you using?
    – Rho
    Jul 25, 2014 at 9:35
  • I am grepping a perl code, when I used perl extension, I got a complete syntax coloring, and I am greping a variable name so it was not even colored. Jul 25, 2014 at 9:37
0

select the contents of the terminal and right click inside the terminal. In the options you can find "copy as HTML". Select the option and paste the copied contents to a file and save it as as an html file. opening the html file using any html renderers (eg. browser). You will be presented with the same contents and colours as your terminal has. tested and verified in ubuntu 20.04 LTS.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .