Saving output of a grep into a file with colors


19 down vote favorite

7

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.


5 down vote

Depending on what you‘re wanting to do with the output file, it‘s possible to add colors to 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.

时间: 2024-12-22 03:44:12

Saving output of a grep into a file with colors的相关文章

grep:Binary file (standard input) matches

grep "key" xxx.log时输出 Binary file xxx.log matches 百度了一下:grep认为这是二进制文件,解决方案:grep -a. grep -a "key" xxx.log

【转】 grep 文件报错 “Binary file ... matches”

原文链接 http://blog.csdn.net/yaochunnian/article/details/7261006 grep 文件报错 “Binary file ... matches” 原因:文件为binary文件 解决:strings vers.log.2010-03-09 | grep -i ‘mezimedia’ 或者 grep -a -i ‘mezimedia’ vers.log.2010-03-09 grep命令是linux下的行过滤工具,其参数繁多,下面就一一介绍个个参数的

Save output to a text file from Mac terminal

  Simply with output redirection: system_profiler > file.txt Basically, this will take the output of system_profiler and save it to the file file.txt. There are technically two different output "streams", standard output, and standard error.

[Java in NetBeans] Lesson 17. File Input/Output.

这个课程的参考视频和图片来自youtube. 主要学到的知识点有: We want to handle the bad Error. (e.g bad input / bugs in program) 1. File() : A Java representation of a file. File file = new File("test.txt"); 2. PrintWriter() : Write to a file. Write string into the file. /

Linux学习笔记之grep命令及sed 命令相关选项

#grep  强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来. 根据模式,搜索文本 ,并将符合模式的文本行显示出来,常与正则表达式相结合使用. [[email protected] ~]# grep --h 用法: grep [选项]... PATTERN [FILE]... 在每个 FILE 或是标准输入中查找 PATTERN. 默认的 PATTERN 是一个基本正则表达式(缩写为 BRE). 例如: grep -i 'hello world' menu.h main.c

grep 命令过滤配置文件中的注释和空行

grep 用法 Usage: grep [OPTION]... PATTERN [FILE]... Search for PATTERN in each FILE or standard input. PATTERN is, by default, a basic regular expression (BRE). Example: grep -i 'hello world' menu.h main.c Regexp selection and interpretation: -E, --ext

Linux 常用命令sed/awk/grep及正则表达式

linux命令sed和awk sed 主要功能 sed,stream editor.是一个"非交互式"字符流编辑器.输入流通过程序并输出到标准输出端. sed主要用来自动编辑一个或者多个文件(替换,插入,删除,追加,更改) 常见应用 抽区域 匹配正则表达式 比较域 增加,附加,替换 执行过程 sed一次处理一行或多行内容.处理时,把当前处理的行存储在临时缓冲区中,称为"模式空间"(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区

Linux下文本搜索工具grep命令使用入门

grep命令入门 如果想通过使用grep命令来实现理想化的文本搜索,对正则表达式的了解是比不可少的.文献1对正则表达式语法做了一个简单的介绍,文献2提供了一个简单的入门.码农也可以自己google一下其他的参考资料.下面就grep命令的使用做个入门级的介绍. 1.1 grep命令的变种 linux下除了grep命令可以完成文本搜索外,还存在egrep,fgrep,rgrep三个命令.这三个命令都是由grep加上一些控制参数演变而来,如egrep=grep -E, fgrep=grep -F, r

N天学习一个Linux命令之帮助命令:grep

前言任何系统都会出问题,出了问题一般怎么排查BUG?这个时候程序中记录的异常日志以及关键节点的日志就非常重要了,面对一大堆的日志文件,怎么找出我们需要的有用信息呢?linux中可以使用grep命令查找,这个命令的功能非常强大,也是我平时中排查线上错误时使用最多的命令之一. 命令名称grep 用途查找指定文件内包含指定关键字(正则表达式)的内容,按行为单位匹配 使用格式grep [OPTIONS] PATTERN [FILE...] 常用选项-V (显示命令版本) 正则模式匹配版本-E, --ex