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. vim配置与使用

    Vim 是一个上古神器,本篇文章主要持续总结使用 Vim 的过程中不得不了解的一些指令和注意事项,以及持续分享一个前端工作者不得不安装的一些插件,而关于 Vim 的简介,主题的选择,以及为何使用 vi ...

  2. Jquery 取值,赋值学习总结

    <h2>获取和设置文本框值:</h2> <input type="button" value="赋值文件框" id="v ...

  3. 目标检测 — Inception-ResNet-v2

    这篇文章介绍的网络有Inception V1.Inception V2.Inception V3.Inception V4与Inception-ResNet-V2. 1.Inception V1 主要 ...

  4. POJ 1504,ZOJ 2001,UVA 713, Adding Reversed Numbers,错误,已找到错误

    ------------------------------------------------------------ 以此题警告自己: 总结, 1.在数组的使用时,一定别忘了初始化 2.在两种情况 ...

  5. 原生js监听input值改变事件

    哈哈哈,又来了,今天闲来无事,实验了下原生js监听input value值改变事件,下面就来说道说道: 本来写监听input值便获是用jquery的,之前的随笔写了,就是这个方法,地址:http:// ...

  6. utc时间、本地时间及时间戳转化

    1.时间戳的概念 时间戳的定义请看百科unix时间戳,需要注意的时间戳为当前时刻减去UTC时间(1970.1.1)零点时刻的秒数差,与当前系统所处的时区无关,同一时刻不管在任何时区下得到的时间戳都是一 ...

  7. NOI 模拟赛 #2

    得分非常惨惨,半个小时写的纯暴力 70 分竟然拿了 rank 1... 如果 OYJason 和 wxjor 在可能会被爆踩吧 嘤 T1 欧拉子图 给一个无向图,如果一个边集的导出子图是一个欧拉回路, ...

  8. netsh wlan start hostednetwork

    常用的无线网命令: 一.开启系统承载网络模块和新建无线网络命令: netsh wlan set hostednetwork mode=allow ssid=wifi key=12345678说明:ss ...

  9. logback个人使用配置

    提供一个目前个人供词使用的,无需日志汇总的日志配置文件: <?xml version="1.0" encoding="UTF-8"?> <co ...

  10. Spring 学习十四 Spring security安全

    Spring security: 我用过的安全机制:   oauth2, filter,  secured方法保护 9.2  保护web请求: 9.2.1  代理Servlet过滤器: Delegat ...