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.

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.

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

  1. grep的用法,小技巧,模板中含有\t时:grep -P "^\t" file

    linux中grep和find的用法区别 本文章详细的介绍了关于在linux中的grep和find两个命令的用法介绍,以及后面总结了它们两年用法区别哦. 先我们来介绍一下关于grep用法和一些小注意事 ...

  2. grep:Binary file (standard input) matches

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

  3. What makes grep consider a file to be binary?

    grep -a worked for me: $ grep --help [...] -a, --text equivalent to --binary-files=text

  4. 转:Intellij idea Version Control File Status Colors ( 版本控制文件状态颜色 )

    https://blog.csdn.net/Bruce_Lee__/article/details/80261308 Added —— 添加 Added in not active changelis ...

  5. Save output to a text file from Mac terminal

      Simply with output redirection: system_profiler > file.txt Basically, this will take the output ...

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

    这个课程的参考视频和图片来自youtube. 主要学到的知识点有: We want to handle the bad Error. (e.g bad input / bugs in program) ...

  7. 【转载】Save terminal output to a file

    To write the output of a command to a file, there are basically 10 commonly used ways. Overview: Ple ...

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

    grep 用法 Usage: grep [OPTION]... PATTERN [FILE]... Search for PATTERN in each FILE or standard input. ...

  9. Linux grep命令和正则表达式

    介绍 grep是一个功能强大的文本搜索命令,可以用它来搜索某个文件中是否包含指定的搜索内容,它可以利用正则表达式来做复杂的筛选操作,它还可以为其它命令传输给管道的筛选,比如我们常用到的分析单个进程的操 ...

随机推荐

  1. 本地连接出现"已启用检测该状态的服务"解决方法、方案

    1.运行 输出dcomcnfg 2.组件服务-计算机-我的电脑-DCOM配置-netprofm 3.右键属性-安全-启动和激活权限-自定义 4.编辑-添加-输入对象名称来选择-输入“LOCAL SER ...

  2. Qt之界面实现技巧-- 窗体显示,绘制背景,圆角,QSS样式

    转自 --> http://blog.sina.com.cn/s/blog_a6fb6cc90101dech.html 总结一下,在开发Qt的过程中的一些技巧!可遇而不可求... 一.主界面 1 ...

  3. java套接字实现接口访问

    是学校博客上的:http://blog.csdn.net/z69183787/article/details/17580325

  4. Linux- 关于windows和Linux和Mac的换行符

    windows 的换行符为"\r\n" Linux的换行符为"\n" Mac的换行符为"\n\r",和Windows相反

  5. HTML5学习笔记——1

    1.视频 <vedio src  controls  height width autoplay loop preload > </vedio> <source src ...

  6. STL stl_uninitialized.h

    stl_uninitialized.h // Filename: stl_uninitialized.h // Comment By: 凝霜 // E-mail: mdl2009@vip.qq.com ...

  7. 如何查看myeclipse是否激活

    myEclipse---->Subscription information--->Subscription expiration date 看这个日期到什么时候!另外建议别用太高版本的M ...

  8. ffmpeg用法(心得体会还有你见过的用法)

    ffmpeg的常用用法很多,我这里提供的用法有可能有许多地方是你没见过的. 一.ffmpeg合并视频 我经常需要切割再把一些零碎的视频给拼接起来,这样可以省许多磁盘空间.其实用mencoder挺不错的 ...

  9. codevs1060 搞笑世界杯

    题目描述 Description 随着世界杯小组赛的结束,法国,阿根廷等世界强队都纷纷被淘汰,让人心痛不已. 于是有 人组织了一场搞笑世界杯,将这些被淘汰的强队重新组织起来和世界杯一同比赛.你和你的朋 ...

  10. Unity3D中的Coroutine及其使用(延时、定时调用函数)

    http://blog.csdn.net/nizihabi/article/details/47606887 一.Coroutine(协程)的概念和本质 在网上的一些资料当中,一直将Coroutine ...