Saving output of a grep into a file with colors
2015-05-02 00:50
246 查看
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 catbut when I open it in vimI 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. |
相关文章推荐
- /usr/bin/ld: i386 architecture of input file `regcomp.o' is incompatible with i386:x86-64 output
- ld: i386 architecture of input file `exit.o' is incompatible with i386:x86-64 output
- linux下出现architecture of input file `*.o' is incompatible with i386:x86-64 output的解决方法
- usr/bin/ld: i386 architecture of input file XXX.a( xxx.o) is incompatible with i386:x86-64 output
- ld: i386 architecture of input file `eatsyscall.o' is incompatible with i386:x86-64 output
- 解决无限 This file is indented with tabs instead of 4 spaces
- resource is out of sync with the file system的解决办法
- eclipse: Resource is out of sync with the file system
- imageNamed/ imageWithContentsOfFile /imageWithData 的区别
- 【Tools.Eclipse】Resource is out of sync with the file system解决办法
- eclipse中出现Resource is out of sync with the file system问题
- iOS NSString 截取,objectAtIndex,rangeOfString,stringWithContentsOfFile,NSEnumerator
- android studio3.0(二)Cannot set the value of read-only property 'outputFile'
- Resource is out of sync with the file system解决办法
- 解决eclipse中出现Resource is out of sync with the file system问题
- 解决无限 This file is indented with tabs instead of 4 spaces
- Saving a Canvas into a PNG file – WP8
- iOS: NSData的方法dataWithContentsOfFile:options:error:
- [UIImage imageWithContentsOfFile:]引发的图片显示异常问题(一半图片是黑的)
- Zend Studio 开发Resource is out of sync with the file system的解决办法