How to write Stdout to a file with colors?


Introduction

The tools we can use to send standard output to files while maintaining their colours will be covered in this post. This is especially helpful while troubleshooting because it makes it simpler to scan the output logs thanks to the coloured areas.

By Using Grep

A file can be searched for text patterns or strings using the grep command. Regular expressions are the names given to the patterns.

Syntax

The syntax for the grep command is as follows −

$ grep [options] pattern [files]

Example

Let's make a example.txt file by using cat command. After that we will use the file with grep command

$ cat >> example.txt
tutorials point rocks

Let's colourize the matching string with grep and then send the output to a different file −

$ grep --color=always "tutorials point rocks" example.txt > grep_output.txt

Output

Output will be saved to grep_output.txt. You can see the content of the file by using cat command −

$ grep --color=always "tutorials point rocks" example.txt > grep_output.txt
$ cat grep_output.txt
tutorials point rocks

In order to keep ANSI sequences when sending the output to a file, we utilised grep's - color=always option. When piped to a file, grep by default removes colours from the output.

By Using tee

The tee command is a tool that simultaneously takes standard input, writes to one or more files, and outputs to standard output

Through the use of piping, we'll combine it with a number of other commands.

Syntax

The tee command syntax is as follows −

$ tee [option]…[file]…

Example

To create some coloured output, we'll use the echo command; to store the output, we'll pipe it with the tee command.

$ echo -e "\e[1;32m Tutorial point rocks \e[0m" | tee -a tee_output.txt

Output

You will get this output −

$ echo -e "\e[1;32m Tutorial point rocks \e[0m" | tee -a tee_output.txt 
Tutorial point rocks

As a simple illustration, we've used tee to store coloured output from an echo command. Similar to other commands that generate coloured output or logs, we can pipe tee with them.

By Using echo

The text characters that we supply as parameters are displayed by the echo command.

The following escape sequences can be used with echo to specify the colour of the content in our file −

$ echo -e "\e[1;45m tutorials point rocks \e[0m" > echo_output.txt

Example

By doing this, the text is coloured and saved to the echo output.txt file. Let's look at it with cat command −

$ cat echo_output.txt

Output

You will get the output −

tutorials point rocks

By Using script

The command script is used to create a typescript of every terminal activity. Until we stop it, it keeps a record of every activity, including inputs and outputs.

By default, it stores the data as a typescript file. But if we want to save the output to a different file, we can specify an argument.

Two files are used by the script command: one for terminal outputs and the other for timing information.

Syntax

Let's look at the script command's syntax −

$ script [options] [file]

Example

Lets run the command given below

$ script -q /dev/null -c "echo -e '\e[1;45m tutorials point rocks \e[0m'" > script_output.txt

We won't receive any output from this, but we can use cat to see what is in the newly generated file −

$ cat script_output.txt

Output

tutorials point rocks 

Conclusion

The tools we can use to keep colours while saving standard output or stdout to a file were discussed in this post. We have discussed grep, tee ,script and echo tools in details. There are various other methods also to keep colors while saving stdout to a file that you can discover after mastering the tools which are given above.

Updated on: 21-Nov-2022

595 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements