Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to store the result of grep in a file with the formatting in the same way as the output in Konsole(Kubuntu). I tried to save it in a LibreOffice doc but the result is no different then the result in text file. How do i do store the result in the file with the formatting? I mean how do i store the result with the colored output?

share|improve this question

migrated from Oct 27 '12 at 8:45

This question came from our site for professional and enthusiast programmers.

To preserve the colors, use --color=always

grep --color=always "pattern" file.txt > newfile.txt

From the man page:

--color[=WHEN], --colour[=WHEN]
      Surround the matched (non-empty) strings, matching lines, context  lines,  file  names,
      line  numbers,  byte  offsets,  and separators (for fields and groups of context lines)
      with escape sequences to display them in color on the terminal.  The colors are defined
      by   the   environment  variable  GREP_COLORS.   The  deprecated  environment  variable
      GREP_COLOR is still supported, but its setting does not have priority.  WHEN is  never,
      always, or auto.
share|improve this answer
This did not work. When i open the file i get unformatted output. Its only when i use cat that i get the output I want. That's not what I wish to do. – Parth Shah Oct 30 '12 at 6:11
@ParthShah: How are you actually reading/viewing your file? You'll find that the above stores the formatted output exactly how you describe (notice how when you cat your file, it prints the formatted output to your terminal). The program you're using to read/view your file may be altering the file's formatting or cannot simply understand it. – Steve Oct 30 '12 at 6:17
grep -rn --color=always pattern * > temp.doc Then I use Libre Office 3 to open my doc. I assume that Libre Office 3 is not able to understand the format. Which app can I use to view the output? – Parth Shah Oct 30 '12 at 8:41
@ParthShah: I use vim to do all my editing of files. Unfortunately I have not found a way to let vim easily "pass-through" all the escape codes that make up the formatting. I have however found this script which can parse the escape codes and uses the vim syntax highlighting to recreate the colour codes in the output. According to its description, it should do exactly what you want (but I have not tested it). If you're just looking to view your files you can scroll through them using: less -R newfile.txt – Steve Oct 30 '12 at 11:17
@ParthShah: Read this: – Steve Oct 30 '12 at 11:20

By default, grep turns color formatting off when piping or redirecting. You can force it to give color-formatted output with the --color=always option:

grep --color=always "pattern" infile > outfile

The color information will show up as a bunch escaped characters. In case you want to remove the escaped characters later on, here's a nifty shell command with sed to do just that:

sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"

(Note, on OS X, use sed -E "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g")

share|improve this answer
sed displays output on the shell. When I copy it to the file the text is displayed without the escape characters. I guess it is not possible to do what i wish to do using grep – Parth Shah Oct 30 '12 at 6:16

You must log in to answer this question.

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