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. 最短路N题Tram SPFA

     #include <algorithm>#include <queue>#include <cstdio>#include <cstdlib>#inc ...

  2. mac下搭建前端自动化工程

    好多年没有接触前端,发现前端行业发生了巨大的变化,很多新鲜术语,比如node.git.grunt.less.sass.预编译.自动化.模块化等等,看得让人晕头转向,不要担心,我会把这之前之后学习成果都 ...

  3. matlab查找最临近搜索knnsearch

    Idx = knnsearch(X,Y) finds the nearest neighbor in X for each query point in Y and returns the indic ...

  4. IE6、IE7、IE8及其他浏览器多个元素并排显示

    IE6.IE7.IE8及其他浏览器多个元素并排显示 HTML代码 <div class="line"> <h1>全部input框</h1> &l ...

  5. django使用bootstrap前端框架

    一.下载bootstrap相关文件,放在项目目录中.在blog 应用中新建static目录,bootstrap文件放在此目录下. bootstrap下载网址:http://v3.bootcss.com ...

  6. 分享知识-快乐自己:Struts2 前台日期到后台的日期格式转换

    案例目录: 关键代码展示: DateConverter: package com.mlq.util; import com.opensymphony.xwork2.conversion.TypeCon ...

  7. javascript-JQuery样式篇(一)

    轻量级的JavaScript库,核心依然是JavaScript,不仅兼容了CSS3,还兼容了各种浏览器 强大的选择器,完善的事件机制,出色的Ajax封装,丰富的UI 进入官方网站获取最新的版本 htt ...

  8. python 爬取腾讯视频评论

    import urllib.request import re import urllib.error headers=('user-agent','Mozilla/5.0 (Windows NT 1 ...

  9. Excel文本获取拼音

    [说明] 版本:Excel 2010 文件后缀:.xls 有在.xlsb文件下使用未成功.建议使用.xls后缀. 1.调出“开发工具” 步骤:文件-->选项-->自定义功能区-->勾 ...

  10. Oracle 12c 多租户在 CDB 中 Plug A PDB,Unplugging A PDB

    Oracle 数据库 12 c 中介绍了多租户选项允许单个容器数据库 (CDB) 来承载多个单独的可插拔数据库 (PDB).本文简单的演示了如何在 CDB 中 Plug A PDB,Unpluggin ...